void test_content() { rng_type * rng = rng_alloc(MZRAN , INIT_DEFAULT); matrix_type * PC = matrix_alloc( 3 , 10); matrix_type * PC_obs = matrix_alloc( 3 , 1 ); double_vector_type * singular_values = double_vector_alloc(3 , 1); matrix_random_init( PC , rng ); matrix_random_init( PC_obs , rng ); { pca_plot_data_type * data = pca_plot_data_alloc("KEY" , PC , PC_obs, singular_values); for (int i=0; i < matrix_get_rows( PC ); i++) { const pca_plot_vector_type * vector = pca_plot_data_iget_vector( data , i ); test_assert_double_equal( matrix_iget( PC_obs , i , 0) , pca_plot_vector_get_obs_value( vector ) ); test_assert_double_equal( double_vector_iget( singular_values , i), pca_plot_vector_get_singular_value( vector ) ); for (int j=0; j < matrix_get_columns( PC ); j++) test_assert_double_equal( matrix_iget( PC , i , j ) , pca_plot_vector_iget_sim_value( vector , j )); test_assert_int_equal( matrix_get_columns( PC ) , pca_plot_vector_get_size( vector )); } pca_plot_data_free( data ); } double_vector_free( singular_values ); matrix_free( PC ); matrix_free( PC_obs ); }
void* pique_thread(void* arg) { pique_ctx_t* ctx = arg; seq_t* seq = seq_create(); twobit_t* tb = twobit_alloc(); rng_t* rng = rng_alloc(1234); bool r; while (true) { pthread_mutex_lock(ctx->f_mutex); if (ctx->fmt == INPUT_FMT_FASTA) r = fasta_read(ctx->f, seq); else if (ctx->fmt == INPUT_FMT_FASTQ) r = fastq_read(ctx->f, seq); pthread_mutex_unlock(ctx->f_mutex); if (!r) break; /* TODO: remove sequences with Ns? */ twobit_copy_str_n(tb, seq->seq.s, seq->seq.n); dbg_add_twobit_seq(ctx->G, rng, tb); } rng_free(rng); seq_free(seq); return NULL; }
void test_diag_std() { const int N = 25; double_vector_type * data = double_vector_alloc( 0,0); rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); matrix_type * m = matrix_alloc( N , N ); double sum1 = 0; double sum2 = 0; int i; for (i=0; i < N; i++) { double R = rng_get_double( rng ); matrix_iset(m , i , i , R); double_vector_iset( data , i , R ); sum1 += R; sum2 += R*R; } { double mean = sum1 / N; double std = sqrt( sum2 / N - mean * mean ); test_assert_double_equal( std , matrix_diag_std( m , mean )); test_assert_double_equal( statistics_std( data ) , matrix_diag_std( m , mean )); test_assert_double_equal( statistics_mean( data ) , mean ); } matrix_free( m ); rng_free( rng ); }
int main( int argc, /* number of arguments */ char *argv[] /* arguments */){ int j,k; unsigned int p; myuint *X=rng_alloc(); //myuint X[N+1]; fprintf(stderr,"Welcome to the Yerevan random number generator!\nThe curent matrix size is %u\n" "(the actual matrix is not kept in memory in this new efficient implementation)",N); fprintf(stderr,"\nWe will now generate a power of our matrix A^m\n "); fprintf(stderr,"\nHow many iterations m do you want?\n(2^10=1024, 2^20=1048576, 2^30=1073741824)\nEnter m: "); scanf("%u", &p); printf("\nHere is convenient Mathematica Input:\n AT={"); for (k=1; (k<=N); k++) { seedvector(X,k); for (j=0; (j<p); j++) { IT(X); } printf("\n{"); printf("%llu", X[1] ); for (j=2; (j<=N); j++) { printf(",\t%llu", X[j] ); } printf("},"); } printf("\b} \n A = Transpose[AT] \n d = Det[A] \n AM = Mod[A, 2^61-1] \n cp = CharacteristicPolynomial[AM, x] \n " "N[Solve[cp == 0, x]] \n N[Eigenvalues[AM]] \n Abs[%%]\n" "M1K = Mod[MatrixPower[A, 2^10], 2^61 - 1]\n "); //\n Maxima input: eq:charpoly(N,x);\n p:radcan(eq); r:allroots(p); printf("\n\nok\n"); rng_free(X); return 1; }
void test_det2() { matrix_type * m = matrix_alloc(2,2); rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); matrix_random_init( m , rng ); { double det2 = matrix_det2( m ); double det = matrix_det( m ); test_assert_double_equal( det , det2 ); } matrix_free( m ); rng_free( rng ); }
void test_det4() { matrix_type * m = matrix_alloc(4 , 4 ); rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); for (int i=0; i < 10; i++) { matrix_random_init( m , rng ); { double det4 = matrix_det4( m ); double det = matrix_det( m ); test_assert_double_equal( det , det4 ); } } matrix_free( m ); rng_free( rng ); }
int main(int argc , char ** argv) { const int queue_timeout = 180; const int submit_timeout = 180; const int status_timeout = 180; const int number_of_jobs = 250; const int submit_threads = number_of_jobs / 10 ; const int status_threads = number_of_jobs + 1; const char * job = util_alloc_abs_path(argv[1]); rng_type * rng = rng_alloc( MZRAN , INIT_CLOCK ); test_work_area_type * work_area = test_work_area_alloc("job_queue"); job_type **jobs = alloc_jobs( rng , number_of_jobs , job); job_queue_type * queue = job_queue_alloc(number_of_jobs, "OK", "ERROR"); queue_driver_type * driver = queue_driver_alloc_local(); job_queue_manager_type * queue_manager = job_queue_manager_alloc( queue ); job_queue_set_driver(queue, driver); job_queue_manager_start_queue(queue_manager, 0, false , true); { thread_pool_type * status_pool = thread_pool_alloc( status_threads , true ); thread_pool_type * submit_pool = thread_pool_alloc( submit_threads , true ); submit_jobs( queue , number_of_jobs , jobs , submit_pool ); status_jobs( queue , number_of_jobs , jobs , status_pool ); if (!thread_pool_try_join( submit_pool , submit_timeout )) util_exit("Joining submit pool failed \n"); thread_pool_free( submit_pool ); job_queue_submit_complete(queue); if (!thread_pool_try_join( status_pool , status_timeout)) util_exit("Joining status pool failed \n"); thread_pool_free( status_pool ); } if (!job_queue_manager_try_wait(queue_manager , queue_timeout)) util_exit("job_queue never completed \n"); job_queue_manager_free(queue_manager); job_queue_free(queue); queue_driver_free(driver); check_jobs( number_of_jobs , jobs ); test_work_area_free(work_area); rng_free( rng ); }
void test_column_equal() { matrix_type * m1 = matrix_alloc(5,5); matrix_type * m2 = matrix_alloc(5,5); matrix_type * m3 = matrix_alloc(6,5); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); matrix_random_init( m1 , rng ); matrix_assign( m2 , m1 ); test_assert_true( matrix_columns_equal( m1 , 2 , m2 , 2 )); test_assert_false( matrix_columns_equal( m1 , 2 , m2 , 3 )); test_assert_false( matrix_columns_equal( m1 , 2 , m3 , 3 )); rng_free( rng ); matrix_free( m1 ); matrix_free( m2 ); matrix_free( m3 ); }
void test_resize() { matrix_type * m1 = matrix_alloc(5,5); matrix_type * m2 = matrix_alloc(5,5); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); matrix_random_init( m1 , rng ); matrix_assign( m2 , m1 ); test_assert_true( matrix_equal( m1 , m2 )); matrix_resize( m1 , 5 , 5 , false ); test_assert_true( matrix_equal( m1 , m2 )); matrix_resize( m1 , 5 , 5 , true ); test_assert_true( matrix_equal( m1 , m2 )); rng_free( rng ); matrix_free( m1 ); matrix_free( m2 ); }
int main(int argc , char ** argv) { rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); { int val1 = rng_get_int( rng , MAX_INT); int val2 = rng_get_int( rng , MAX_INT); test_assert_int_not_equal( val1 , val2 ); rng_init( rng , INIT_DEFAULT ); val2 = rng_get_int( rng , MAX_INT ); test_assert_int_equal( val1 , val2 ); } { int val2 , val1; int state_size = rng_state_size( rng ); char * buffer1 = util_calloc( state_size , sizeof * buffer1 ); char * buffer2 = util_calloc( state_size , sizeof * buffer2 ); test_assert_int_not_equal( state_size , 0 ); test_assert_int_equal( state_size , MZRAN_STATE_SIZE ); rng_init( rng , INIT_DEFAULT ); rng_get_state( rng , buffer1 ); val1 = rng_get_int( rng , MAX_INT); val2 = rng_get_int( rng , MAX_INT); test_assert_int_not_equal( val1 , val2 ); rng_set_state( rng , buffer1 ); val2 = rng_get_int( rng , MAX_INT); test_assert_int_equal( val1 , val2 ); rng_init( rng , INIT_DEFAULT ); rng_get_state( rng , buffer2 ); test_assert_mem_equal( buffer1 , buffer2 , state_size ); val2 = rng_get_int( rng , MAX_INT); rng_get_state( rng , buffer2 ); test_assert_mem_not_equal( buffer1 , buffer2 , state_size ); free( buffer1 ); free( buffer2 ); } rng_free( rng ); exit(0); }
ert_test_context_type * ert_test_context_alloc( const char * test_name , const char * model_config , const char * site_config) { ert_test_context_type * test_context = util_malloc( sizeof * test_context ); UTIL_TYPE_ID_INIT( test_context , ERT_TEST_CONTEXT_TYPE_ID ); if (util_file_exists(model_config)) { test_context->work_area = test_work_area_alloc(test_name); test_work_area_set_store( test_context->work_area , false ); test_work_area_copy_parent_content(test_context->work_area , model_config ); { char * config_file = util_split_alloc_filename( model_config ); test_context->enkf_main = enkf_main_bootstrap( site_config , config_file , true , false ); free( config_file ); } test_context->rng = rng_alloc( MZRAN , INIT_DEV_URANDOM ); } else { test_context->enkf_main = NULL; test_context->work_area = NULL; test_context->rng = NULL; } return test_context; }
void test_readwrite() { test_work_area_type * test_area = test_work_area_alloc("matrix-test"); { rng_type * rng = rng_alloc(MZRAN , INIT_DEV_URANDOM ); matrix_type * m1 = matrix_alloc(3 , 3); matrix_type * m2 = matrix_alloc(3 , 3); matrix_random_init( m1 , rng ); matrix_assign(m2 , m1); test_assert_true( matrix_equal( m1 , m2 ) ); { FILE * stream = util_fopen("m1" , "w"); matrix_fwrite( m1 , stream ); fclose( stream ); } matrix_random_init( m1 , rng ); test_assert_false( matrix_equal( m1 , m2 ) ); { FILE * stream = util_fopen("m1" , "r"); matrix_free( m1 ); m1 = matrix_alloc(1,1); printf("-----------------------------------------------------------------\n"); matrix_fread( m1 , stream ); test_assert_int_equal( matrix_get_rows(m1) , matrix_get_rows( m2)); test_assert_int_equal( matrix_get_columns(m1) , matrix_get_columns( m2)); util_fseek( stream , 0 , SEEK_SET); { matrix_type * m3 = matrix_fread_alloc( stream ); test_assert_true( matrix_equal( m2 , m3 )); matrix_free( m3 ); } fclose( stream ); } test_assert_true( matrix_equal( m1 , m2 ) ); matrix_free( m2 ); matrix_free( m1 ); rng_free( rng ); } test_work_area_free( test_area ); }
void test_state() { rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int ens_size = 10; int active_size = 8; int rows = 100; matrix_type * state = matrix_alloc(1,1); bool_vector_type * ens_mask = bool_vector_alloc(ens_size , false); matrix_type * A = matrix_alloc( rows , active_size); matrix_type * A2 = matrix_alloc( rows, active_size ); matrix_type * A3 = matrix_alloc( 1,1 ); for (int i=0; i < active_size; i++) bool_vector_iset( ens_mask , i + 1 , true ); matrix_random_init(A , rng); rml_enkf_common_store_state( state , A , ens_mask ); test_assert_int_equal( matrix_get_rows( state ) , rows ); test_assert_int_equal( matrix_get_columns( state ) , ens_size ); { int g; int a = 0; for (g=0; g < ens_size; g++) { if (bool_vector_iget( ens_mask , g )) { test_assert_true( matrix_columns_equal( state , g , A , a )); a++; } } } rml_enkf_common_recover_state( state , A2 , ens_mask); rml_enkf_common_recover_state( state , A3 , ens_mask); test_assert_true( matrix_equal( A , A2 )); test_assert_true( matrix_equal( A , A3 )); bool_vector_free( ens_mask ); matrix_free( state ); matrix_free( A ); }
int main( int argc, char ** argv) { rng_type * rng = rng_alloc( MZRAN , INIT_DEV_RANDOM ); matrix_type * A = matrix_alloc( 12 , 12 ); matrix_type * B = matrix_alloc( 12 , 12 ); matrix_random_init( A , rng ); matrix_assign( B , A ); matrix_pretty_print( A , " A " , "%8.4f" ); #ifdef WITH_LAPACK matrix_inv( B ); printf("\n"); matrix_pretty_print( B , "inv(A)" , "%8.4f" ); matrix_inplace_matmul( B , A ); printf("\n"); matrix_pretty_print( B , " I " , "%8.4f" ); { matrix_type * A3 = matrix_alloc(3,3); matrix_random_init( A3 , rng ); matrix_iset( A3 , 0 , 0 , sin(0.98)); printf("matrix_det3:%g ",matrix_det3( A3 )); printf("matrix_det:%g \n",matrix_det( A3 )); } { matrix_type * A4 = matrix_alloc(4,4); matrix_random_init( A4 , rng ); matrix_iset( A4 , 0 , 0 , sin(0.98)); printf("matrix_det4:%g ",matrix_det4( A4 )); printf("matrix_det:%g \n",matrix_det( A4 )); } #endif matrix_free( A ); matrix_free( B ); rng_free( rng ); }
int main(int argc, char* argv[]) { size_t step_size = 1; while (1) { int opt = getopt(argc, argv, "hs:"); if (opt == -1) break; switch (opt) { case 's': step_size = (size_t) strtoul(optarg, NULL, 10); break; case 'h': print_usage(stdout); return EXIT_SUCCESS; case '?': return EXIT_FAILURE; default: abort(); } } if (optind >= argc) { print_usage(stderr); return EXIT_FAILURE; } const char* fn = argv[optind]; FILE* f = fopen(fn, "r"); if (f == NULL) { fprintf(stderr, "Can't open %s for reading.\n", fn); return EXIT_FAILURE; } fprintf(stderr, "Reading adjacency matrix ... "); /* some rather brittle parsing of matrix market files */ char buffer[512]; fgets(buffer, sizeof(buffer), f); if (strcmp(buffer, "%%MatrixMarket matrix coordinate integer general\n") != 0) { fprintf(stderr, "Error: Incorrectly formatted matrix market file.\n"); return EXIT_FAILURE; } unsigned int n, n2, m; fscanf(f, "%u %u %u\n", &n, &n2, &m); edge_array_t E; E.size = m; E.m = 0; E.es = malloc_or_die(E.size * sizeof(edge_t)); edge_t e; while (fgets(buffer, sizeof(buffer), f)) { sscanf(buffer, "%u %u %u\n", &e.u, &e.v, &e.w); e.u -= 1; e.v -= 1; /* make 0-based */ push_edge(&E, &e); } fclose(f); rng_t* rng = rng_alloc(); shuffle(rng, E.es, E.m); rng_free(rng); qsort(E.es, E.m, sizeof(edge_t), edge_cmp); fprintf(stderr, "done. (%zu edges)\n", E.m); unsigned int* ds = malloc_or_die(n * sizeof(unsigned int)); size_t i; for (i = 0; i < E.m; i += step_size) { size_t c = count_components(E.es + i, n, E.m - i, ds); printf("%zu\n", c); fflush(stdout); } free(ds); free(E.es); return EXIT_SUCCESS; }
int main( int argc , char ** argv) { int num_kw = 1000; // Total file size should roughly exceed 2GB int kw_size = 600000; ecl_kw_type * kw = ecl_kw_alloc("KW" , kw_size , ECL_INT_TYPE ); rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int i; offset_type file_size; for (i=0; i < kw_size; i++) ecl_kw_iset_int( kw , i , rng_get_int( rng , 912732 )); { fortio_type * fortio = fortio_open_writer( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw; i++) { printf("Writing keyword %d/%d to file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fwrite( kw , fortio ); } fortio_fclose( fortio ); } /*{ fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); for (i = 0; i < num_kw - 1; i++) { printf("SKipping keyword %d/%d from file:LARGE_FILE.UNRST \n",i+1 , num_kw ); ecl_kw_fskip( fortio ); } { ecl_kw_type * file_kw = ecl_kw_fread_alloc( fortio ); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_kw_free( file_kw ); } fortio_fclose( fortio ); } */ file_size = util_file_size( "LARGE_FILE.UNRST" ); printf("File size: %lld \n",file_size); { fortio_type * fortio = fortio_open_reader( "LARGE_FILE.UNRST" , false , ECL_ENDIAN_FLIP); printf("Seeking to file end: "); fortio_fseek( fortio , file_size , SEEK_SET); fortio_fclose( fortio ); printf("Seek OK \n"); } printf("Doing ecl_file_open(..)\n"); { ecl_file_type * file = ecl_file_open( "LARGE_FILE.UNRST" , 0); ecl_kw_type * file_kw = ecl_file_iget_named_kw( file , "KW" , num_kw - 1); if (ecl_kw_equal( kw , file_kw )) printf("Keyword read back from file correctly :-) \n"); else printf("Fatal error - keyword different on return ...\n"); ecl_file_close( file ); } remove( "LARGE_FILE.UNRST" ); exit(0); }
analysis_config_type * create_analysis_config() { rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); analysis_config_type * ac = analysis_config_alloc( rng ); return ac; }
rng_type * rng_config_alloc_init_rng( const rng_config_type * rng_config ) { rng_type * rng = rng_alloc(rng_config_get_type(rng_config) , INIT_DEFAULT); return rng_config_init_rng__(rng_config, rng); }