| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation, either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
|
| 18 |
#include "plink2_random.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
double RandNormal(sfmt_t* sfmtp, double* secondval_ptr) { |
| 25 |
// Box-Muller. try changing this to e.g. ziggurat if it's ever a serious |
| 26 |
// bottleneck. |
| 27 |
const double dxx = sqrt(-2 * log(RandUnif(sfmtp))); |
| 28 |
const double dyy = (2 * kPi) * RandUnif(sfmtp); |
| 29 |
*secondval_ptr = dxx * cos(dyy); |
| 30 |
return dxx * sin(dyy); |
| 31 |
} |
| 32 |
|
| 33 |
BoolErr InitAllocSfmtpArr(uint32_t thread_ct, uint32_t use_main_sfmt_as_element_zero, sfmt_t* sfmtp, sfmt_t*** sfmtp_arrp) { |
| 34 |
if (unlikely(BIGSTACK_ALLOC_X(sfmt_t*, thread_ct, sfmtp_arrp))) { |
| 35 |
return 1; |
| 36 |
} |
| 37 |
sfmt_t** sfmtp_arr = *sfmtp_arrp; |
| 38 |
if (use_main_sfmt_as_element_zero) { |
| 39 |
sfmtp_arr[0] = sfmtp; |
| 40 |
} |
| 41 |
if (thread_ct > use_main_sfmt_as_element_zero) { |
| 42 |
uint32_t uibuf[4]; |
| 43 |
for (uint32_t tidx = use_main_sfmt_as_element_zero; tidx != thread_ct; ++tidx) { |
| 44 |
if (unlikely(BIGSTACK_ALLOC_X(sfmt_t, 1, &(sfmtp_arr[tidx])))) { |
| 45 |
return 1; |
| 46 |
} |
| 47 |
for (uint32_t uii = 0; uii != 4; ++uii) { |
| 48 |
uibuf[uii] = sfmt_genrand_uint32(sfmtp); |
| 49 |
} |
| 50 |
sfmt_init_by_array(sfmtp_arr[tidx], uibuf, 4); |
| 51 |
} |
| 52 |
} |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
typedef struct FillGaussianDArrCtxStruct { |
| 57 |
sfmt_t** sfmtp_arr; |
| 58 |
uintptr_t entry_pair_ct; |
| 59 |
|
| 60 |
double* dst; |
| 61 |
} FillGaussianDArrCtx; |
| 62 |
|
| 63 |
void FillGaussianDArrMain(uintptr_t tidx, uintptr_t thread_ct, FillGaussianDArrCtx* ctx) { |
| 64 |
const uintptr_t entry_pair_ct = ctx->entry_pair_ct; |
| 65 |
sfmt_t* sfmtp = ctx->sfmtp_arr[tidx]; |
| 66 |
// 32-bit overflow fix (12 Oct 2019): forgot to cast to uint64_t |
| 67 |
const uintptr_t idx_start = (S_CAST(uint64_t, tidx) * entry_pair_ct) / thread_ct; |
| 68 |
const uintptr_t idx_ct = ((S_CAST(uint64_t, tidx + 1) * entry_pair_ct) / thread_ct) - idx_start; |
| 69 |
double* dst_iter = &(ctx->dst[idx_start * 2]); |
| 70 |
for (uintptr_t ulii = 0; ulii != idx_ct; ++ulii) { |
| 71 |
double dxx; |
| 72 |
*dst_iter++ = RandNormal(sfmtp, &dxx); |
| 73 |
*dst_iter++ = dxx; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
THREAD_FUNC_DECL FillGaussianDArrThread(void* raw_arg) { |
| 78 |
ThreadGroupFuncArg* arg = S_CAST(ThreadGroupFuncArg*, raw_arg); |
| 79 |
FillGaussianDArrCtx* ctx = S_CAST(FillGaussianDArrCtx*, arg->sharedp->context); |
| 80 |
FillGaussianDArrMain(arg->tidx, GetThreadCt(arg->sharedp) + 1, ctx); |
| 81 |
THREAD_RETURN; |
| 82 |
} |
| 83 |
|
| 84 |
PglErr FillGaussianDArr(uintptr_t entry_pair_ct, uint32_t thread_ct, sfmt_t* sfmtp, double* darray) { |
| 85 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 86 |
PglErr reterr = kPglRetSuccess; |
| 87 |
ThreadGroup tg; |
| 88 |
PreinitThreads(&tg); |
| 89 |
FillGaussianDArrCtx ctx; |
| 90 |
{ |
| 91 |
const uintptr_t max_useful_thread_ct = DivUp(entry_pair_ct, 262144); |
| 92 |
if (thread_ct > max_useful_thread_ct) { |
| 93 |
thread_ct = max_useful_thread_ct; |
| 94 |
} |
| 95 |
if (unlikely(SetThreadCt0(thread_ct - 1, &tg) || |
| 96 |
InitAllocSfmtpArr(thread_ct, 1, sfmtp, &ctx.sfmtp_arr))) { |
| 97 |
goto FillGaussianDArr_ret_NOMEM; |
| 98 |
} |
| 99 |
ctx.dst = darray; |
| 100 |
ctx.entry_pair_ct = entry_pair_ct; |
| 101 |
if (thread_ct > 1) { |
| 102 |
SetThreadFuncAndData(FillGaussianDArrThread, &ctx, &tg); |
| 103 |
DeclareLastThreadBlock(&tg); |
| 104 |
if (unlikely(SpawnThreads(&tg))) { |
| 105 |
goto FillGaussianDArr_ret_THREAD_CREATE_FAIL; |
| 106 |
} |
| 107 |
} |
| 108 |
FillGaussianDArrMain(thread_ct - 1, thread_ct, &ctx); |
| 109 |
JoinThreads0(&tg); |
| 110 |
} |
| 111 |
while (0) { |
| 112 |
FillGaussianDArr_ret_NOMEM: |
| 113 |
reterr = kPglRetNomem; |
| 114 |
break; |
| 115 |
FillGaussianDArr_ret_THREAD_CREATE_FAIL: |
| 116 |
reterr = kPglRetThreadCreateFail; |
| 117 |
break; |
| 118 |
} |
| 119 |
CleanupThreads(&tg); |
| 120 |
BigstackReset(bigstack_mark); |
| 121 |
return reterr; |
| 122 |
} |
| 123 |
|
| 124 |
typedef struct RandomizeArenaCtxStruct { |
| 125 |
sfmt_t** sfmtp_arr; |
| 126 |
|
| 127 |
// assumed to be at least 8-byte-aligned |
| 128 |
unsigned char* arena_bottom; |
| 129 |
unsigned char* arena_top; |
| 130 |
} RandomizeArenaCtx; |
| 131 |
|
| 132 |
void RandomizeArenaMain(uintptr_t tidx, uintptr_t thread_ct, RandomizeArenaCtx* ctx) { |
| 133 |
unsigned char* arena_bottom = ctx->arena_bottom; |
| 134 |
unsigned char* arena_top = ctx->arena_top; |
| 135 |
const uint64_t arena_int64_ct = S_CAST(uintptr_t, arena_top - arena_bottom) / sizeof(int64_t); |
| 136 |
uint64_t* arena_bottom_alias = R_CAST(uint64_t*, arena_bottom); |
| 137 |
assert(arena_int64_ct >= thread_ct); |
| 138 |
const uint64_t start_idx = RoundDownPow2((tidx * arena_int64_ct) / thread_ct, kInt64PerCacheline); |
| 139 |
uint64_t end_idx = ((tidx + 1) * arena_int64_ct) / thread_ct; |
| 140 |
if (tidx + 1 != thread_ct) { |
| 141 |
end_idx = RoundDownPow2(end_idx, kInt64PerCacheline); |
| 142 |
} |
| 143 |
sfmt_t* sfmtp = ctx->sfmtp_arr[tidx]; |
| 144 |
for (uintptr_t ulii = start_idx; ulii != end_idx; ++ulii) { |
| 145 |
arena_bottom_alias[ulii] = sfmt_genrand_uint64(sfmtp); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
THREAD_FUNC_DECL RandomizeArenaThread(void* raw_arg) { |
| 150 |
ThreadGroupFuncArg* arg = S_CAST(ThreadGroupFuncArg*, raw_arg); |
| 151 |
RandomizeArenaCtx* ctx = S_CAST(RandomizeArenaCtx*, arg->sharedp->context); |
| 152 |
RandomizeArenaMain(arg->tidx, GetThreadCt(arg->sharedp) + 1, ctx); |
| 153 |
THREAD_RETURN; |
| 154 |
} |
| 155 |
|
| 156 |
PglErr RandomizeBigstack(uint32_t thread_ct, sfmt_t* sfmtp) { |
| 157 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 158 |
PglErr reterr = kPglRetSuccess; |
| 159 |
ThreadGroup tg; |
| 160 |
PreinitThreads(&tg); |
| 161 |
RandomizeArenaCtx ctx; |
| 162 |
{ |
| 163 |
if (thread_ct > 16) { |
| 164 |
thread_ct = 16; |
| 165 |
} |
| 166 |
if (unlikely(SetThreadCt0(thread_ct - 1, &tg) || |
| 167 |
InitAllocSfmtpArr(thread_ct, 1, sfmtp, &ctx.sfmtp_arr))) { |
| 168 |
goto RandomizeBigstack_ret_NOMEM; |
| 169 |
} |
| 170 |
ctx.arena_bottom = g_bigstack_base; |
| 171 |
ctx.arena_top = g_bigstack_end; |
| 172 |
if (thread_ct > 1) { |
| 173 |
SetThreadFuncAndData(RandomizeArenaThread, &ctx, &tg); |
| 174 |
DeclareLastThreadBlock(&tg); |
| 175 |
if (unlikely(SpawnThreads(&tg))) { |
| 176 |
goto RandomizeBigstack_ret_THREAD_CREATE_FAIL; |
| 177 |
} |
| 178 |
} |
| 179 |
RandomizeArenaMain(thread_ct - 1, thread_ct, &ctx); |
| 180 |
JoinThreads0(&tg); |
| 181 |
// now ensure the bytes reserved by InitAllocSfmtpArr() are also properly |
| 182 |
// randomized (some of them already are, but there are gaps) |
| 183 |
uint64_t* initial_segment_end = R_CAST(uint64_t*, g_bigstack_base); |
| 184 |
for (uint64_t* initial_segment_iter = R_CAST(uint64_t*, bigstack_mark); initial_segment_iter != initial_segment_end; ++initial_segment_iter) { |
| 185 |
*initial_segment_iter = sfmt_genrand_uint64(sfmtp); |
| 186 |
} |
| 187 |
} |
| 188 |
while (0) { |
| 189 |
RandomizeBigstack_ret_NOMEM: |
| 190 |
reterr = kPglRetNomem; |
| 191 |
break; |
| 192 |
RandomizeBigstack_ret_THREAD_CREATE_FAIL: |
| 193 |
reterr = kPglRetThreadCreateFail; |
| 194 |
break; |
| 195 |
} |
| 196 |
CleanupThreads(&tg); |
| 197 |
BigstackReset(bigstack_mark); |
| 198 |
return reterr; |
| 199 |
} |
| 200 |
|
| 201 |
void GeneratePerm1Interleaved(uint32_t tot_bit_ct, uint32_t set_bit_ct, uintptr_t perm_start_idx, uintptr_t perm_end_idx, uintptr_t* perm_buf, sfmt_t* sfmtp) { |
| 202 |
assert(tot_bit_ct > 1); |
| 203 |
const uintptr_t tot_bit_ctl = BitCtToWordCt(tot_bit_ct); |
| 204 |
const uintptr_t perm_ct = perm_end_idx - perm_start_idx; |
| 205 |
const uint32_t tot_quotient = 0x100000000LLU / tot_bit_ct; |
| 206 |
const uint32_t upper_bound = tot_bit_ct * tot_quotient - 1; |
| 207 |
uint32_t totq_preshift; |
| 208 |
uint64_t totq_magic; |
| 209 |
uint32_t totq_postshift; |
| 210 |
uint32_t totq_incr; |
| 211 |
// seeing as how we're gonna divide by the same number a billion times or so, |
| 212 |
// it just might be worth optimizing that division... |
| 213 |
DivisionMagicNums(tot_quotient, &totq_magic, &totq_preshift, &totq_postshift, &totq_incr); |
| 214 |
if (set_bit_ct * 2 < tot_bit_ct) { |
| 215 |
for (uintptr_t widx = 0; widx != tot_bit_ctl; ++widx) { |
| 216 |
ZeroWArr(perm_ct, &(perm_buf[perm_start_idx + (widx * perm_end_idx)])); |
| 217 |
} |
| 218 |
for (uintptr_t perm_idx = perm_start_idx; perm_idx != perm_end_idx; ++perm_idx) { |
| 219 |
uintptr_t* pbptr = &(perm_buf[perm_idx]); |
| 220 |
for (uint32_t num_set = 0; num_set != set_bit_ct; ++num_set) { |
| 221 |
uintptr_t widx; |
| 222 |
uintptr_t shifted_bit; |
| 223 |
do { |
| 224 |
uint32_t urand; |
| 225 |
do { |
| 226 |
urand = sfmt_genrand_uint32(sfmtp); |
| 227 |
} while (urand > upper_bound); |
| 228 |
// this is identical to lowbits = urand / tot_quotient |
| 229 |
uintptr_t lowbits = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift; |
| 230 |
widx = lowbits / kBitsPerWord; |
| 231 |
lowbits &= (kBitsPerWord - 1); |
| 232 |
shifted_bit = k1LU << lowbits; |
| 233 |
} while (pbptr[widx * perm_end_idx] & shifted_bit); |
| 234 |
pbptr[widx * perm_end_idx] |= shifted_bit; |
| 235 |
} |
| 236 |
} |
| 237 |
} else { |
| 238 |
for (uintptr_t widx = 0; widx != tot_bit_ctl; ++widx) { |
| 239 |
SetAllWArr(perm_ct, &(perm_buf[perm_start_idx + (widx * perm_end_idx)])); |
| 240 |
} |
| 241 |
// "set" has reversed meaning here |
| 242 |
set_bit_ct = tot_bit_ct - set_bit_ct; |
| 243 |
for (uintptr_t perm_idx = perm_start_idx; perm_idx != perm_end_idx; ++perm_idx) { |
| 244 |
uintptr_t* pbptr = &(perm_buf[perm_idx]); |
| 245 |
for (uint32_t num_set = 0; num_set != set_bit_ct; ++num_set) { |
| 246 |
uintptr_t widx; |
| 247 |
uintptr_t shifted_bit; |
| 248 |
do { |
| 249 |
uint32_t urand; |
| 250 |
do { |
| 251 |
urand = sfmt_genrand_uint32(sfmtp); |
| 252 |
} while (urand > upper_bound); |
| 253 |
uintptr_t lowbits = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift; |
| 254 |
widx = lowbits / kBitsPerWord; |
| 255 |
lowbits &= (kBitsPerWord - 1); |
| 256 |
shifted_bit = k1LU << lowbits; |
| 257 |
} while (!(pbptr[widx * perm_end_idx] & shifted_bit)); |
| 258 |
pbptr[widx * perm_end_idx] ^= shifted_bit; |
| 259 |
} |
| 260 |
} |
| 261 |
const uint32_t remaining_bit_ct = tot_bit_ct % kBitsPerWord; |
| 262 |
if (remaining_bit_ct) { |
| 263 |
const uintptr_t final_mask = (~k0LU) >> (kBitsPerWord - remaining_bit_ct); |
| 264 |
uintptr_t* pbptr = &(perm_buf[(tot_bit_ctl - 1) * perm_end_idx + perm_start_idx]); |
| 265 |
for (uintptr_t perm_idx = perm_start_idx; perm_idx != perm_end_idx; ++perm_idx) { |
| 266 |
*pbptr &= final_mask; |
| 267 |
pbptr++; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
#ifdef __cplusplus |
| 274 |
} |
| 275 |
#endif |