Skip to content

Commit

Permalink
pretty comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cbouilla committed Nov 17, 2023
1 parent de2bcf1 commit 6903e63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/spasm_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "spasm.h"

/* This PRNG is SHA256 in counter mode... */
/* This PRNG is SHA256 in counter mode */

static void rehash(spasm_prng_ctx *ctx)
{
Expand All @@ -15,6 +15,9 @@ static void rehash(spasm_prng_ctx *ctx)
ctx->i = 0;
}

/*
* Return a uniformly random 32-bit integer
*/
u32 spasm_prng_u32(spasm_prng_ctx *ctx)
{
if (ctx->i == 8)
Expand All @@ -24,6 +27,9 @@ u32 spasm_prng_u32(spasm_prng_ctx *ctx)
return htonl(res);
}

/*
* Return a uniformly integer modulo prime (rejection sampling)
*/
spasm_ZZp spasm_prng_ZZp(spasm_prng_ctx *ctx)
{
for (;;) {
Expand Down Expand Up @@ -54,9 +60,8 @@ void spasm_prng_seed(const u8 *seed, i64 prime, u32 seq, spasm_prng_ctx *ctx)
rehash(ctx);
}


/*
* Seed must be 32 bytes.
* In case where a 32-byte seed (i.e. a SHA256 digest) is not available
*/
void spasm_prng_seed_simple(i64 prime, u64 seed, u32 seq, spasm_prng_ctx *ctx)
{
Expand Down
18 changes: 9 additions & 9 deletions src/spasm_schur.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,41 +361,41 @@ void spasm_schur_dense_randomized(const struct spasm_csr *A, const int *p, int n
#pragma omp parallel
{
/* per-thread scratch space */
spasm_ZZp *x = spasm_malloc(m * sizeof(*x));
spasm_ZZp *y = spasm_malloc(m * sizeof(*y));

#pragma omp for schedule(dynamic, verbose_step)
for (i64 k = 0; k < N; k++) {
spasm_prng_ctx ctx;
spasm_prng_seed_simple(prime, k, 0, &ctx);

for (int j = 0; j < m; j++)
x[j] = 0;
y[j] = 0;
if (w <= 0) {
/* x <--- random linear combinations of all rows */
for (int i = 0; i < n; i++) {
int inew = p[i];
int coeff = spasm_prng_ZZp(&ctx);
spasm_scatter(A, inew, coeff, x);
spasm_scatter(A, inew, coeff, y);
}
} else {
for (int i = 0; i < w; i++) {
int inew = p[rand() % n];
int coeff = (i == 0) ? 1 : spasm_prng_ZZp(&ctx);
spasm_scatter(A, inew, coeff, x);
spasm_scatter(A, inew, coeff, y);
}
}

/* eliminate known sparse pivots */
for (int i = 0; i < U->n; i++) {
int j = Uj[Up[i]];
if (x[j] == 0)
int j = Uj[Up[i]]; // warning: this assumes pivots are first entries on the row
if (y[j] == 0)
continue;
spasm_scatter(U, i, -x[j], x);
spasm_scatter(U, i, -y[j], y);
}

/* gather x into S[k] */
void *Sk = row_pointer(S, Sm, datatype, k);
gather(Sm, q, x, Sk, datatype);
gather(Sm, q, y, Sk, datatype);
// for (int j = 0; j < Sm; j++) {
// int jj = q[j];
// spasm_datatype_write(S, k * Sm + j, datatype, x[jj]);
Expand All @@ -407,7 +407,7 @@ void spasm_schur_dense_randomized(const struct spasm_csr *A, const int *p, int n
fflush(stderr);
}
}
free(x);
free(y);
}
fprintf(stderr, "\n[schur/dense/random] finished in %.1fs\n", spasm_wtime() - start);
}

0 comments on commit 6903e63

Please sign in to comment.