LIS_INT lis_solver_set_shadowresidual(LIS_SOLVER solver, LIS_VECTOR r0, LIS_VECTOR rs0) { unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; LIS_INT i,n,resid; LIS_DEBUG_FUNC_IN; resid = solver->options[LIS_OPTIONS_INIT_SHADOW_RESID]; if( resid==LIS_RANDOM ) { n = solver->A->n; init_by_array(init, length); #ifdef USE_QUAD_PRECISION if( solver->precision==LIS_PRECISION_DEFAULT ) #endif { #ifdef _OPENMP #pragma omp parallel for private(i) #endif for(i=0;i<n;i++) { rs0->value[i] = genrand_real1(); } } #ifdef USE_QUAD_PRECISION else { #ifdef _OPENMP #pragma omp parallel for private(i) #endif for(i=0;i<n;i++) { rs0->value[i] = genrand_real1(); rs0->value_lo[i] = 0.0; } } #endif } else { #ifdef USE_QUAD_PRECISION if( solver->precision==LIS_PRECISION_DEFAULT ) #endif { lis_vector_copy(r0,rs0); } #ifdef USE_QUAD_PRECISION else { lis_vector_copyex_mm(r0,rs0); } #endif } LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; }
void gretl_rand_set_multi_seed (const gretl_matrix *seed) { guint32 useeds[4]; int i; for (i=0; i<4; i++) { useeds[i] = seed->val[i]; } init_by_array(useeds, 4); gretl_rand_octet(NULL); }
int* makeArray( int size ) { unsigned long init[4] = { 0x123, 0x234, 0x345, 0x456 }; unsigned long length = 10; init_by_array( init, length ); int*array = ( int* )malloc( sizeof(int) * size ); if( !array ) return NULL; int*p = array; for( int i = 0; i < size; i++ ) *p++ = abs( (int) genrand_int32() >> 16 ); return array; }
void init_triangular_table(void) { int i; unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; init_by_array(init, length); for (i = 0; i < 257; i++) { triangular_table[i] = (double)(i/* - (genrand_int32() % 1)*/) / 256.0; if(triangular_table[i] < 0) {triangular_table[i] = 0;} else if(triangular_table[i] > 1.0) {triangular_table[i] = 1.0;} } triangular_table[0] = 0.0; triangular_table[256] = 1.0; }
main() { int i, j; clock_t before, after; // Synthesizable Random Number Initialization - mt19937ar unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; //***Mod init_by_array(init, length); //***Mod // printf( "Initialization...." ); char f = 'c'; double s = 3.0; double x = 1.0; double t = 1.0; double r = 5.0; double b = 1.5; double a[NUM_PASSES]; // printf( "....done\nStarting test........\n" ); before = clock(); BlackScholesWrapper(f, s, x, t, r, b, a); after = clock(); // Mean, Variance and Confidence Calculations double sum = 0; double mean = 0; double variance = 0; for (i = 0; i < NUM_PASSES; i++) { sum += a[i]; } mean = sum/NUM_PASSES; for(i = 0; i < NUM_PASSES; i++){ variance += (a[i]-mean)*(a[i]-mean)/(double)NUM_PASSES; } double conf_width = 1.96 * sqrt(variance) / sqrt ((double) NUM_PASSES); // printf ("Black-Scholes benchmark:\n" // "------------------------\n" // "S %.2f\n" // "T %.2f\n" // "r %.2f\n" // "b %.2f\n", // 3.0, 1, 5.0, 1.5); printf("Run#: %d, Mean: %f, Variance: %f, Confidence Interval: (%f, %f)\n", NUM_PASSES, mean, variance, mean-conf_width, mean+conf_width); // printf( "Simulation Time in secs: %f\n", ((double)(after-before))/CLOCKS_PER_SEC ); return(0); }
int main ( int argc, char * const argv[] ) { printf( "Testing output and speed of inventors' Mersenne Twister program\n" ); printf( "\nTest of random integer generation:\n" ); unsigned long oneSeed = 4357UL; unsigned long bigSeed[4] = { 0x123UL, 0x234UL, 0x345UL, 0x456UL }; printf( "\nTest of random integer generation:\n" ); init_by_array( bigSeed, 4 ); unsigned long i; for( i = 0; i < 1000UL; ++i ) { printf( "%10lu ", genrand_int32() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of random real number [0,1) generation:\n" ); for( i = 0; i < 1000UL; ++i ) { printf( "%10.8f ", genrand_real2() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of random real number [0,1] generation:\n" ); init_genrand( oneSeed ); for( i = 0; i < 2000UL; ++i ) { printf( "%10.8f ", genrand_real1() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of time to generate one billion random integers:\n" ); init_genrand( oneSeed ); unsigned long junk = 0; clock_t start = clock(); for( i = 0; i < 1000000000UL; ++i ) { junk ^= genrand_int32(); } clock_t stop = clock(); if( junk == 0 ) printf( "jinx\n" ); printf( "Time elapsed = " ); printf( "%8.3f", double( stop - start ) / CLOCKS_PER_SEC ); printf( " s\n" ); return 0; }
int test_rand_save_state() { const unsigned nrand=10; unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; double* rands; unsigned i; ellipsis_mt19937_rng* rand; rands=(double*)malloc(nrand*sizeof(double)); rand=(ellipsis_mt19937_rng*)malloc(sizeof(ellipsis_mt19937_rng)); init_by_array(rand,init, length); /* store the first half */ for(i=0;i<nrand/2;++i) { rands[i]=genrand_uniform(rand); } /* save the state */ save_rand_state(rand,"myrand.dat"); /* store the 2nd half */ for(i=0;i<nrand/2;++i) { rands[nrand/2+i]=genrand_uniform(rand); } /* read the state */ read_rand_state(rand,"myrand.dat"); /* check if the 2nd half is the same */ for(i=0;i<nrand/2;++i) { if(rands[nrand/2+i]!=genrand_uniform(rand)) return EXIT_FAILURE; } free(rand); return EXIT_SUCCESS; }
int main(void) { unsigned long oneSeed = 4357UL; unsigned long bigSeed[4] = { 0x123, 0x234, 0x345, 0x456 }; printf( "Testing output and speed of mt19937ar-c*k.c\n" ); printf( "\nTest of random integer generation:\n" ); init_by_array( bigSeed, 4 ); for( i = 0; i < 1000; ++i ) { printf( "%10lu ", genrand_int32() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of random real number [0,1) generation:\n" ); for( i = 0; i < 1000; ++i ) { printf( "%10.8f ", genrand_real2() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of random real number [0,1] generation:\n" ); init_genrand( oneSeed ); for( i = 0; i < 1000; ++i ) { printf( "%10.8f ", genrand_real1() ); if( i % 5 == 4 ) printf("\n"); } printf( "\nTest of time to generate 300 million random integers:\n" ); init_genrand( oneSeed ); start = clock(); for( i = 0; i < 300000000; ++i ) { junk = genrand_int32(); } stop = clock(); printf( "Time elapsed = " ); printf( "%8.3f", (double)( stop - start ) / CLOCKS_PER_SEC ); printf( " s\n" ); return 0; }
static int seed_rng (lua_State *L) { nl_RNG *r = getrng(L); if (lua_isnoneornil(L, 1)) init_genrand(r, RNG_SEED); else if (lua_isnumber(L, 1)) /* seed? */ init_genrand(r, lua_tointeger(L, 1)); else { /* vector */ unsigned long initkey[RNG_MAXSTATES]; int i, k; lua_Number *e; nl_Matrix *m = nl_checkmatrix(L, 1); checkrvector(L, m, 1); for (i = 0, e = m->data; i < m->size; i++, e += m->stride) { lua_number2int(k, *e); initkey[i] = (unsigned long) k; } init_by_array(r, initkey, m->size); } return 0; }
int main(void) { int i; unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; init_by_array(init, length); /* This is an example of initializing by an array. */ /* You may use init_genrand(seed) with any 32bit integer */ /* as a seed for a simpler initialization */ printf("1000 outputs of genrand_int32()\n"); for (i=0; i<1000; i++) { printf("%10lu ", genrand_int32()); if (i%5==4) printf("\n"); } printf("\n1000 outputs of genrand_real2()\n"); for (i=0; i<1000; i++) { printf("%10.8f ", genrand_real2()); if (i%5==4) printf("\n"); } return 0; }
int main( int argc, char ** argv ) { std::string OutputFileName( argv[ 1 ] ); if( argc >= 4 ) { unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; init_by_array(init, length); for( int i = 0; i < 10; ++i ) { Simulation Australia( atol( argv[ 2 ] ) ); Australia.Start( OutputFileName.c_str( ), atoi( argv[ 3 ] ) ); Australia.Stop( ); OutputFileName += "1"; } } else { std::cout << "Error: missing arguments" << std::endl; std::cout << "Reminder: Simulation OutputFileName MaxNumberOfRabbits LifeTimeInMonths" << std::endl; } return 0; }
int test_random_uni() { unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; double s=0; double s2=0; double u; double mu; double var; const unsigned long nrand=10000000; unsigned long i; ellipsis_mt19937_rng* rand; rand=(ellipsis_mt19937_rng*)malloc(sizeof(ellipsis_mt19937_rng)); init_by_array(rand,init, length); /* find the mean and the variance */ for(i=0;i<nrand;++i) { u=genrand_uniform(rand); s+=u; s2+=u*u; } mu=s/(double)nrand; var=(s2-s*s/(double)nrand)/(double)(nrand-1); /* mean should be 0.5 and the variance should be 1/12 */ if(fabs(mu - 0.5)>1e-3 || fabs(var-1./12.)>1e-3) { free(rand); return EXIT_FAILURE; } free(rand); return EXIT_SUCCESS; }
int main(int argc, char **argv) { tap_n((3+5)*4 + (2+5)*2 + (2+5)); if (argc > 1) { long s = atol(argv[1]); init_by_array((uint32_t*)&s, sizeof s / sizeof(uint32_t)); } else { time_t s = time(NULL); init_by_array((uint32_t*)&s, sizeof s / sizeof(uint32_t)); } void *data = malloc(DATA_LEN); random_fill(data, DATA_LEN); const size_t codepoints = DATA_LEN * CHAR_BIT / 21; const mushcell pos = MUSHCELL_MAX - WRAP_AFTER; mushcoords beg = MUSHCOORDS(pos,pos,pos), end; void *space_buf = malloc(mushspace_sizeof); mushspace *space; bool ok; mushbounds bounds, expected_loose, expected_tight; size_t spaces_beg, spaces_end; codepoint_reader cp_reader; #define BOUNDS_CHECK \ mushspace_get_loose_bounds(space, &bounds); \ \ tap_leqcos(bounds.beg, expected_loose.beg, \ "get_loose_bounds reports appropriate beg", \ "get_loose_bounds reports too large beg"); \ tap_geqcos(bounds.end, expected_loose.end, \ "get_loose_bounds reports appropriate end", \ "get_loose_bounds reports too small end"); \ \ ok = mushspace_get_tight_bounds(space, &bounds); \ tap_bool(ok, "get_tight_bounds says that the space is nonempty", \ "get_tight_bounds says that the space is empty"); \ \ tap_eqcos(bounds.beg, expected_tight.beg, \ "get_tight_bounds reports correct beg", \ "get_tight_bounds reports incorrect beg"); \ tap_eqcos(bounds.end, expected_tight.end, \ "get_tight_bounds reports correct end", \ "get_tight_bounds reports incorrect end"); \ #define LOAD_STRING(suf, T, ENCODER, BLOWUP, FOREACH_CP) \ space = mushspace_init(space_buf, NULL); \ if (!space) { \ tap_not_ok("init returned null"); \ tap_skip_remaining("init failed"); \ return 1; \ } \ tap_ok("init succeeded"); \ \ T *encoded_data##suf = (T*)data; \ size_t encoded_len##suf = DATA_LEN / sizeof *encoded_data##suf; \ if (BLOWUP) { \ encoded_data##suf = \ malloc((codepoints * BLOWUP) * sizeof *encoded_data##suf); \ encoded_len##suf = ENCODER(data, encoded_data##suf, codepoints); \ } \ \ mushspace_load_string##suf( \ space, encoded_data##suf, encoded_len##suf, &end, beg, true); \ \ if (BLOWUP) \ free(encoded_data##suf); \ \ size_t ii##suf = 0; \ mushcell cp##suf; \ \ spaces_beg = 0; \ spaces_end = 0; \ \ FOREACH_CP(suf) { \ if (ii##suf <= WRAP_AFTER) { \ if (cp##suf == ' ') \ ++spaces_end; \ else \ spaces_end = 0; \ continue; \ } \ if (cp##suf == ' ') \ ++spaces_beg; \ else \ break; \ } \ \ expected_loose.beg = expected_tight.beg = \ MUSHCOORDS(MUSHCELL_MIN, beg.y, beg.z); \ expected_loose.end = expected_tight.end = \ MUSHCOORDS(MUSHCELL_MAX, beg.y, beg.z); \ \ expected_loose.beg.x = expected_tight.beg.x += spaces_beg; \ expected_loose.end.x = expected_tight.end.x -= spaces_end; \ \ tap_eqcos(end, expected_tight.end, \ "load_string" #suf " reports correct end", \ "load_string" #suf " reports incorrect end"); \ \ ok = true; \ FOREACH_CP(suf) { \ mushcell gc = \ mushspace_get(space, mushcoords_add(beg, MUSHCOORDS(ii##suf,0,0))); \ if (gc == cp##suf) \ continue; \ ok = false; \ tap_not_ok("get doesn't match data given to load_string" #suf); \ printf(" ---\n" \ " failed index: %zu\n" \ " expected: %" MUSHCELL_PRIx "\n" \ " got: %" MUSHCELL_PRIx "\n" \ " ...\n", \ ii##suf, cp##suf, gc); \ break; \ } \ if (ok) \ tap_ok("get matches data given to load_string" #suf); \ \ BOUNDS_CHECK; \ mushspace_free(space); #define DIRECT_FOREACH_CP(s) \ ii##s = 0; \ for (; ii##s < encoded_len##s && (cp##s = encoded_data##s[ii##s], true); \ ++ii##s) #define READER_FOREACH_CP(s) \ cp_reader = make_codepoint_reader(data, codepoints); \ for (ii##s = 0; (cp##s = next_codepoint(&cp_reader)) != UINT32_MAX; ++ii##s) LOAD_STRING(, unsigned char, dummy, 0, DIRECT_FOREACH_CP); LOAD_STRING(_cell, mushcell, dummy, 0, DIRECT_FOREACH_CP); LOAD_STRING(_utf8, uint8_t, encode_utf8, 4, READER_FOREACH_CP); LOAD_STRING(_utf16, uint16_t, encode_utf16, 2, READER_FOREACH_CP); const mushcell *data_cell = (const mushcell*)data; const size_t data_cell_count = DATA_LEN / sizeof *data_cell; spaces_beg = 0; spaces_end = 0; for (size_t i = WRAP_AFTER+1; i < data_cell_count && data_cell[i++] == ' ';) ++spaces_beg; for (size_t i = WRAP_AFTER+1; i-- > 0 && data_cell[i] == ' ';) ++spaces_end; expected_loose.beg = expected_tight.beg = MUSHCOORDS(MUSHCELL_MIN, beg.y, beg.z); expected_loose.end = expected_tight.end = MUSHCOORDS(MUSHCELL_MAX, beg.y, beg.z); expected_loose.beg.x = expected_tight.beg.x += spaces_beg; expected_loose.end.x = expected_tight.end.x -= spaces_beg; #define PUT(FOREACH_CELL, S) \ space = mushspace_init(space_buf, NULL); \ if (!space) { \ tap_not_ok("init returned null"); \ tap_skip_remaining("init failed"); \ return 1; \ } \ tap_ok("init succeeded"); \ FOREACH_CELL { \ mushspace_put(space, mushcoords_add(beg, MUSHCOORDS(i, 0, 0)), \ data_cell[i]); \ } \ \ ok = true; \ for (size_t i = 0; i < data_cell_count; ++i) { \ mushcell dc = data_cell[i], \ gc = mushspace_get(space, \ mushcoords_add(beg, MUSHCOORDS(i, 0, 0))); \ if (gc == dc) \ continue; \ ok = false; \ tap_not_ok("get doesn't match what was put" S); \ printf(" ---\n" \ " failed index: %zu\n" \ " expected: %" MUSHCELL_PRIx "\n" \ " got: %" MUSHCELL_PRIx "\n" \ " ...\n", \ i, dc, gc); \ break; \ } \ if (ok) \ tap_ok("get matches what was put" S); \ \ BOUNDS_CHECK; #define FORWARD for (size_t i = 0; i < data_cell_count; ++i) #define REVERSE for (size_t i = data_cell_count; i--;) PUT(FORWARD, " (forward order)"); mushspace_free(space); PUT(REVERSE, " (reverse order)"); if (!ok) { tap_skip_remaining("won't copy bad space"); return 1; } void *space_buf2 = malloc(mushspace_sizeof); mushspace *space2 = mushspace_copy(space_buf2, space, NULL); mushspace_free(space); free(space_buf); space = space2; if (!space) { tap_not_ok("copy returned null"); tap_skip_remaining("copy failed"); return 1; } tap_ok("copy succeeded"); ok = true; for (size_t i = 0; i < DATA_LEN / sizeof *data_cell; ++i) { mushcell dc = data_cell[i], gc = mushspace_get(space, mushcoords_add(beg, MUSHCOORDS(i, 0, 0))); if (gc == dc) continue; ok = false; tap_not_ok("get in copy doesn't match data"); printf(" ---\n" " failed index: %zu\n" " expected: %" MUSHCELL_PRIx "\n" " got: %" MUSHCELL_PRIx "\n" " ...\n", i, dc, gc); break; } if (ok) tap_ok("get in copy matched data"); BOUNDS_CHECK; free(data); mushspace_free(space); free(space_buf2); }
LIS_INT lis_idr1(LIS_SOLVER solver) { LIS_MATRIX A; LIS_VECTOR b,x; LIS_VECTOR r,t,v,av,*dX,*dR,*P; LIS_SCALAR om, h; LIS_SCALAR M,m,c; LIS_REAL bnrm2, nrm2, tol; LIS_REAL angle; LIS_INT i,j,k,s,oldest; LIS_INT iter,maxiter,n,output,conv; double times,ptimes,tim; unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; LIS_DEBUG_FUNC_IN; A = solver->A; b = solver->b; x = solver->x; n = A->n; maxiter = solver->options[LIS_OPTIONS_MAXITER]; output = solver->options[LIS_OPTIONS_OUTPUT]; conv = solver->options[LIS_OPTIONS_CONV_COND]; s = 1; ptimes = 0.0; r = solver->work[0]; t = solver->work[1]; v = solver->work[2]; av = solver->work[3]; P = &solver->work[4]; dX = &solver->work[4+s]; dR = &solver->work[4+2*s]; angle = 0.7; /* Initial Residual */ if( lis_solver_get_initial_residual(solver,NULL,NULL,r,&bnrm2) ) { LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } tol = solver->tol; init_by_array(init, length); for(i=0;i<n;i++) { P[0]->value[i] = genrand_real1(); } /* lis_vector_copy(r,P[0]); */ lis_idrs_orth(s,P); #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, r, dX[0]); ptimes += lis_wtime()-times; LIS_MATVEC(A,dX[0],dR[0]); #else #ifdef PRE_BOTH times = lis_wtime(); lis_psolve_right(solver, r, t); ptimes += lis_wtime()-times; LIS_MATVEC(A,t,av); lis_vector_print(av); times = lis_wtime(); lis_psolve_left(solver, av, v); ptimes += lis_wtime()-times; #endif #endif /* lis_idrs_omega(dR[k],r,angle,&om); */ lis_vector_dot(dR[0],dR[0],&h); lis_vector_dot(dR[0],r,&om); om = om / h; lis_vector_scale(om,dX[0]); lis_vector_scale(-om,dR[0]); lis_vector_axpy(1.0,dX[0],x); lis_vector_axpy(1.0,dR[0],r); /* convergence check */ lis_solver_get_residual[conv](r,solver,&nrm2); if( output ) { if( output & LIS_PRINT_MEM ) solver->residual[1] = nrm2; if( output & LIS_PRINT_OUT && A->my_rank==0 ) printf("iter: %5d residual = %e\n", 1, nrm2); } if( tol >= nrm2 ) { solver->retcode = LIS_SUCCESS; solver->iter = 1; solver->resid = nrm2; solver->ptimes = ptimes; LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } lis_vector_dot(P[0],dR[0],&M); iter = s; oldest = 0; lis_vector_dot(P[0],r,&m); while( iter<=maxiter ) { tim = lis_wtime(); /* solve Mc=m */ c = m/M; for(i=0;i<n;i++) { v->value[i] = r->value[i] + -c*dR[0]->value[i]; } /* lis_vector_copy(r,v); lis_vector_axpy(-c,dR[0],v); */ #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, v, av); ptimes += lis_wtime()-times; LIS_MATVEC(A,av,t); #else #ifdef PRE_BOTH times = lis_wtime(); lis_psolve_right(solver, v, t); ptimes += lis_wtime()-times; LIS_MATVEC(A,t,av); times = lis_wtime(); lis_psolve_left(solver, av, t); ptimes += lis_wtime()-times; #endif #endif /* lis_idrs_omega(t,v,angle,&om); lis_vector_dot(t,t,&h); lis_vector_dot(t,v,&om); */ h = t->value[0]*t->value[0]; om = t->value[0]*v->value[0]; for(i=1;i<n;i++) { h += t->value[i]*t->value[i]; om += t->value[i]*v->value[i]; } om = om / h; /* printf("i=%d om = %lf\n",iter,om); */ #if 0 lis_vector_scale(-om,t); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dR[j],t); } lis_vector_copy(t,dR[oldest]); lis_vector_scale(om,av); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dX[j],av); } lis_vector_copy(av,dX[oldest]); #else for(i=0;i<n;i++) { h = om*av->value[i]; h -= dX[0]->value[i] * c; dX[0]->value[i] = h; h = -om*t->value[i]; h -= dR[0]->value[i] * c; dR[0]->value[i] = h; } #endif lis_vector_axpy(1.0,dR[0],r); lis_vector_axpy(1.0,dX[0],x); iter++; /* convergence check */ lis_solver_get_residual[conv](r,solver,&nrm2); if( output ) { if( output & LIS_PRINT_MEM ) solver->residual[iter] = nrm2; if( output & LIS_PRINT_OUT && A->my_rank==0 ) printf("iter: %5d residual = %e\n", iter, nrm2); } if( tol >= nrm2 ) { solver->retcode = LIS_SUCCESS; solver->iter = iter; solver->resid = nrm2; solver->ptimes = ptimes; LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } lis_vector_dot(P[0],dR[0],&h); m += h; M = h; /* solve Mc=m */ c = m/M; for(i=0;i<n;i++) { v->value[i] = r->value[i] + -c*dR[0]->value[i]; } /* lis_vector_copy(r,v); lis_vector_axpy(-c,dR[0],v); */ #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, v, av); ptimes += lis_wtime()-times; #endif #if 0 lis_vector_scale(om,av); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dX[j],av); } lis_vector_copy(av,dX[oldest]); #else for(i=0;i<n;i++) { h = om*av->value[i]; h -= dX[0]->value[i] * c; dX[0]->value[i] = h; } #endif LIS_MATVEC(A,dX[0],dR[0]); lis_vector_scale(-1.0,dR[0]); lis_vector_axpy(1.0,dR[0],r); lis_vector_axpy(1.0,dX[0],x); iter++; /* convergence check */ lis_solver_get_residual[conv](r,solver,&nrm2); if( output ) { if( output & LIS_PRINT_MEM ) solver->residual[iter] = nrm2; if( output & LIS_PRINT_OUT && A->my_rank==0 ) printf("iter: %5d residual = %e\n", iter, nrm2); } if( tol >= nrm2 ) { solver->retcode = LIS_SUCCESS; solver->iter = iter; solver->resid = nrm2; solver->ptimes = ptimes; LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } lis_vector_dot(P[0],dR[0],&h); m += h; M = h; tim = lis_wtime() - tim; /* printf("update m,M: %e\n",tim); */ } solver->retcode = LIS_MAXITER; solver->iter = iter; solver->resid = nrm2; LIS_DEBUG_FUNC_OUT; return LIS_MAXITER; }
LIS_INT lis_idrs(LIS_SOLVER solver) { LIS_MATRIX A; LIS_VECTOR b,x; LIS_VECTOR r,t,v,av,*dX,*dR,*P; LIS_SCALAR om, h; LIS_SCALAR *M,*m,*c,*MM; LIS_REAL bnrm2, nrm2, tol; LIS_REAL angle; LIS_INT i,j,k,s,oldest; LIS_INT iter,maxiter,n,output,conv; double times,ptimes,tim; unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; LIS_DEBUG_FUNC_IN; A = solver->A; b = solver->b; x = solver->x; n = A->n; maxiter = solver->options[LIS_OPTIONS_MAXITER]; output = solver->options[LIS_OPTIONS_OUTPUT]; conv = solver->options[LIS_OPTIONS_CONV_COND]; s = solver->options[LIS_OPTIONS_IDRS_RESTART]; ptimes = 0.0; r = solver->work[0]; t = solver->work[1]; v = solver->work[2]; av = solver->work[3]; dX = &solver->work[4]; P = &solver->work[4+s]; dR = &solver->work[4+2*s]; angle = 0.7; m = (LIS_SCALAR *)lis_malloc(s*sizeof(LIS_SCALAR), "lis_idrs::m"); c = (LIS_SCALAR *)lis_malloc(s*sizeof(LIS_SCALAR), "lis_idrs::c"); M = (LIS_SCALAR *)lis_malloc(s*s*sizeof(LIS_SCALAR), "lis_idrs::M"); MM = (LIS_SCALAR *)lis_malloc(s*s*sizeof(LIS_SCALAR), "lis_idrs::M"); /* Initial Residual */ if( lis_solver_get_initial_residual(solver,NULL,NULL,r,&bnrm2) ) { lis_free2(4,m,c,M,MM); LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } tol = solver->tol; init_by_array(init, length); for(k=0;k<s;k++) { for(i=0;i<n;i++) { P[k]->value[i] = genrand_real1(); } } lis_idrs_orth(s,P); for( k=0; k<s; k++ ) { #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, r, dX[k]); ptimes += lis_wtime()-times; LIS_MATVEC(A,dX[k],dR[k]); #endif lis_vector_dot(dR[k],dR[k],&h); lis_vector_dot(dR[k],r,&om); om = om / h; lis_vector_scale(om,dX[k]); lis_vector_scale(-om,dR[k]); lis_vector_axpy(1.0,dX[k],x); lis_vector_axpy(1.0,dR[k],r); /* convergence check */ lis_solver_get_residual[conv](r,solver,&nrm2); if( output ) { if( output & LIS_PRINT_MEM ) solver->residual[k+1] = nrm2; if( output & LIS_PRINT_OUT && A->my_rank==0 ) printf("iter: %5d residual = %e\n", k+1, nrm2); } if( tol >= nrm2 ) { lis_free2(4,m,c,M,MM); solver->retcode = LIS_SUCCESS; solver->iter = k+1; solver->resid = nrm2; solver->ptimes = ptimes; LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } for(i=0;i<s;i++) { lis_vector_dot(P[i],dR[k],&M[k*s+i]); } } iter = s; oldest = 0; for(i=0;i<s;i++) { lis_vector_dot(P[i],r,&m[i]); } while( iter<=maxiter ) { tim = lis_wtime(); lis_array_solve(s,M,m,c,MM); /* solve Mc=m */ lis_vector_copy(r,v); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dR[j],v); } if( (iter%(s+1))==s ) { #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, v, av); ptimes += lis_wtime()-times; LIS_MATVEC(A,av,t); #endif lis_vector_dot(t,t,&h); lis_vector_dot(t,v,&om); om = om / h; #if 0 lis_vector_scale(-om,t); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dR[j],t); } lis_vector_copy(t,dR[oldest]); lis_vector_scale(om,av); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dX[j],av); } lis_vector_copy(av,dX[oldest]); #else for(i=0;i<n;i++) { h = om*av->value[i]; for(j=0;j<s;j++) { h -= dX[j]->value[i] * c[j]; } dX[oldest]->value[i] = h; } for(i=0;i<n;i++) { h = -om*t->value[i]; for(j=0;j<s;j++) { h -= dR[j]->value[i] * c[j]; } dR[oldest]->value[i] = h; } #endif } else { #ifdef PRE_RIGHT times = lis_wtime(); lis_psolve(solver, v, av); ptimes += lis_wtime()-times; #endif #if 0 lis_vector_scale(om,av); for(j=0;j<s;j++) { lis_vector_axpy(-c[j],dX[j],av); } lis_vector_copy(av,dX[oldest]); #else for(i=0;i<n;i++) { h = om*av->value[i]; for(j=0;j<s;j++) { h -= dX[j]->value[i] * c[j]; } dX[oldest]->value[i] = h; } #endif LIS_MATVEC(A,dX[oldest],dR[oldest]); lis_vector_scale(-1.0,dR[oldest]); } lis_vector_axpy(1.0,dR[oldest],r); lis_vector_axpy(1.0,dX[oldest],x); iter++; /* convergence check */ lis_solver_get_residual[conv](r,solver,&nrm2); if( output ) { if( output & LIS_PRINT_MEM ) solver->residual[iter] = nrm2; if( output & LIS_PRINT_OUT && A->my_rank==0 ) printf("iter: %5d residual = %e\n", iter, nrm2); } if( tol >= nrm2 ) { lis_free2(4,m,c,M,MM); solver->retcode = LIS_SUCCESS; solver->iter = iter; solver->resid = nrm2; solver->ptimes = ptimes; LIS_DEBUG_FUNC_OUT; return LIS_SUCCESS; } for(i=0;i<s;i++) { lis_vector_dot(P[i],dR[oldest],&h); m[i] += h; M[oldest*s+i] = h; } oldest++; if( oldest==s ) oldest = 0; tim = lis_wtime() - tim; /* printf("update m,M: %e\n",tim); */ } lis_free2(4,m,c,M,MM); solver->retcode = LIS_MAXITER; solver->iter = iter; solver->resid = nrm2; LIS_DEBUG_FUNC_OUT; return LIS_MAXITER; }
int main(int argc, char **argv) { // We don't check bounds here: we'd end up essentially duplicating the logic // from load_string, and there's little point in that. tap_n(2*4 + 2*2 + 2); if (argc > 1) { long s = atol(argv[1]); init_by_array((uint32_t*)&s, sizeof s / sizeof(uint32_t)); } else { time_t s = time(NULL); init_by_array((uint32_t*)&s, sizeof s / sizeof(uint32_t)); } void *data = malloc(DATA_LEN); random_fill(data, DATA_LEN); const size_t codepoints = DATA_LEN * CHAR_BIT / 21; mushcoords beg = MUSHCOORDS(MUSHCELL_MAX-WRAP_AFTER, MUSHCELL_MAX-WRAP_AFTER, MUSHCELL_MAX-WRAP_AFTER), end; void *space_buf = malloc(mushspace_sizeof); mushspace *space; bool ok; codepoint_reader cp_reader; mushcoords pos, pos_next; bool cr; #define POS_INIT \ pos_next = beg; \ cr = false; #define CP_POS(cp) \ pos = pos_next; \ switch (cp) { \ default: \ if (MUSHSPACE_DIM > 1 && cr) { \ cr = false; \ pos = MUSHCOORDS(beg.x, pos.y + 1, pos.z); \ } \ pos_next = MUSHCOORDS(pos.x + 1, pos.y, pos.z); \ break; \ case '\r': \ if (MUSHSPACE_DIM > 1) { \ if (cr) \ pos_next = pos = MUSHCOORDS(beg.x, pos.y + 1, pos.z); \ cr = true; \ } \ continue; \ case '\n': \ if (MUSHSPACE_DIM > 1) { \ cr = false; \ pos_next = pos = MUSHCOORDS(beg.x, pos.y + 1, pos.z); \ } \ continue; \ case '\f': \ if (MUSHSPACE_DIM > 2) { \ cr = false; \ pos_next = pos = MUSHCOORDS(beg.x, beg.y, pos.z + 1); \ } else if (cr) { \ cr = false; \ pos_next = pos = MUSHCOORDS(beg.x, pos.y + 1, pos.z); \ } \ continue; \ } #define LOAD_STRING(suf, T, ENCODER, BLOWUP, FOREACH_CP) \ space = mushspace_init(space_buf, NULL); \ if (!space) { \ tap_not_ok("init returned null"); \ tap_skip_remaining("init failed"); \ return 1; \ } \ tap_ok("init succeeded"); \ \ T *encoded_data##suf = (T*)data; \ size_t encoded_len##suf = DATA_LEN / sizeof *encoded_data##suf; \ if (BLOWUP) { \ encoded_data##suf = \ malloc((codepoints * BLOWUP) * sizeof *encoded_data##suf); \ encoded_len##suf = ENCODER(data, encoded_data##suf, codepoints); \ } \ \ mushspace_load_string##suf( \ space, encoded_data##suf, encoded_len##suf, &end, beg, false); \ \ if (BLOWUP) \ free(encoded_data##suf); \ \ ok = true; \ POS_INIT; \ FOREACH_CP(suf) { \ CP_POS(cp##suf); \ mushcell gc = mushspace_get(space, pos); \ if (gc == cp##suf) \ continue; \ ok = false; \ tap_not_ok("get doesn't match data given to load_string" #suf); \ printf(" ---\n" \ " expected: %" MUSHCELL_PRIx "\n" \ " got: %" MUSHCELL_PRIx "\n" \ " failed index: %zu\n", \ cp##suf, gc, ii##suf); \ printf(" failed pos relative to min: ("); \ for (uint8_t i = 0; i < MUSHSPACE_DIM; ++i) \ printf(" %" MUSHCELL_PRId, mushcell_sub(pos.v[i], MUSHCELL_MIN)); \ printf(" )\n" \ " ...\n"); \ break; \ } \ if (ok) \ tap_ok("get matches data given to load_string" #suf); \ \ mushspace_free(space); #define DIRECT_FOREACH_CP(s) \ mushcell cp##s; \ size_t ii##s = 0; \ for (; ii##s < encoded_len##s && (cp##s = encoded_data##s[ii##s], true); \ ++ii##s) #define READER_FOREACH_CP(s) \ cp_reader = make_codepoint_reader(data, codepoints); \ size_t ii##s = 0; \ for (mushcell cp##s; (cp##s = next_codepoint(&cp_reader)) != UINT32_MAX; \ ++ii##s) LOAD_STRING(, unsigned char, dummy, 0, DIRECT_FOREACH_CP); LOAD_STRING(_cell, mushcell, dummy, 0, DIRECT_FOREACH_CP); LOAD_STRING(_utf8, uint8_t, encode_utf8, 4, READER_FOREACH_CP); LOAD_STRING(_utf16, uint16_t, encode_utf16, 2, READER_FOREACH_CP); const mushcell *data_cell = (const mushcell*)data; const size_t data_cell_count = DATA_LEN / sizeof *data_cell; mushcoords *saved_pos = malloc(data_cell_count * sizeof *saved_pos); #define PUT(FOREACH_CELL, S, GET_POS, SAVE_POS) \ space = mushspace_init(space_buf, NULL); \ if (!space) { \ tap_not_ok("init returned null"); \ tap_skip_remaining("init failed"); \ free(saved_pos); \ return 1; \ } \ tap_ok("init succeeded"); \ POS_INIT; \ FOREACH_CELL { \ GET_POS(data_cell[i]); \ if (SAVE_POS) \ saved_pos[i] = pos; \ mushspace_put(space, pos, data_cell[i]); \ } \ \ ok = true; \ POS_INIT; \ for (size_t i = 0; i < data_cell_count; ++i) { \ mushcell dc = data_cell[i]; \ GET_POS(dc); \ mushcell gc = mushspace_get(space, pos); \ if (gc == dc) \ continue; \ ok = false; \ tap_not_ok("get doesn't match what was put" S); \ printf(" ---\n" \ " failed index: %zu\n" \ " expected: %" MUSHCELL_PRIx "\n" \ " got: %" MUSHCELL_PRIx "\n", \ i, dc, gc); \ printf(" failed pos relative to min: ("); \ for (uint8_t j = 0; j < MUSHSPACE_DIM; ++j) \ printf(" %" MUSHCELL_PRId, mushcell_sub(pos.v[j], MUSHCELL_MIN)); \ printf(" )\n" \ " ...\n"); \ break; \ } \ if (ok) \ tap_ok("get matches what was put" S); #define FORWARD for (size_t i = 0; i < data_cell_count; ++i) #define REVERSE for (size_t i = data_cell_count; i--;) #define SAVED_POS(_) pos = saved_pos[i]; PUT(FORWARD, " (forward order)", CP_POS, true); mushspace_free(space); PUT(REVERSE, " (reverse order)", SAVED_POS, false); free(saved_pos); if (!ok) { tap_skip_remaining("won't copy bad space"); return 1; } void *space_buf2 = malloc(mushspace_sizeof); mushspace *space2 = mushspace_copy(space_buf2, space, NULL); mushspace_free(space); free(space_buf); space = space2; if (!space) { tap_not_ok("copy returned null"); tap_skip_remaining("copy failed"); return 1; } tap_ok("copy succeeded"); ok = true; POS_INIT; for (size_t i = 0; i < DATA_LEN / sizeof *data_cell; ++i) { mushcell dc = data_cell[i]; CP_POS(dc); mushcell gc = mushspace_get(space, pos); if (gc == dc) continue; ok = false; tap_not_ok("get in copy doesn't match data"); printf(" ---\n" " failed index: %zu\n" " expected: %" MUSHCELL_PRIx "\n" " got: %" MUSHCELL_PRIx "\n", i, dc, gc); printf(" failed pos relative to min: ("); for (uint8_t j = 0; j < MUSHSPACE_DIM; ++j) printf(" %" MUSHCELL_PRId, mushcell_sub(pos.v[j], MUSHCELL_MIN)); \ printf(" )\n" " ...\n"); break; } if (ok) tap_ok("get in copy matched data"); free(data); mushspace_free(space); free(space_buf2); }
void calc_post_equal_weights_samples(live_point* psamps,unsigned num_post_samps,double logz,unsigned num_dim,char* file_name) { unsigned i,k; unsigned num_eff_posts; ellipsis_mt19937_rng* rand; unsigned long rand_init[4]= {0x123, 0x234, 0x345, 0x456}; int rand_length=4; double uni_rand; double S_k; double eff_log_num_samples=0; double* weight=(double*)malloc(num_post_samps*sizeof(double)); double sum_wt; live_point* post_equal_weight_samples; /* allocate memroy for random number generator*/ rand=(ellipsis_mt19937_rng*)malloc(sizeof(ellipsis_mt19937_rng)); init_by_array(rand,rand_init, rand_length); /*calculate effective number of samples */ for(i=0; i<num_post_samps; ++i) { weight[i]=exp(psamps[i].log_weight-logz); if(weight[i]>0) { eff_log_num_samples-=weight[i]*log(weight[i]); } } num_eff_posts=(unsigned)floor((double)exp(eff_log_num_samples)); /*check if it is less than number of posterior samples*/ if(num_post_samps<num_eff_posts) num_eff_posts=num_post_samps; /*allocate eqully weighted posterior samples */ post_equal_weight_samples=(live_point*)malloc(num_eff_posts*sizeof(live_point)); for(i=0; i<num_eff_posts; ++i) { init_live_point(&post_equal_weight_samples[i],num_dim); } /*staircase sampling */ sum_wt=0; k=0; for(i=0; i<num_post_samps; ++i) { uni_rand=genrand_uniform(rand); sum_wt+=weight[i]; S_k=uni_rand+num_eff_posts*sum_wt; if(S_k-(double)k > 1.) { copy_live_point(&post_equal_weight_samples[k],&psamps[i]); ++k; } } /* once again check if have the expected number of posterior samples */ if(k<num_eff_posts) num_eff_posts=k; /*write to txt file*/ write_live_points_to_txt_file(file_name,post_equal_weight_samples,num_eff_posts,num_dim); /*clean up*/ free(rand); free(weight); free(post_equal_weight_samples); }