Ejemplo n.º 1
0
static void test_gradient_descent() {
  BatchData    *batch_data;
  Correction   *correction, *expected;
  NetworkState *network_state;
  BundlePaths  *paths       = bundle_paths_new(1);
  WorkerData   *worker_data = worker_data_new(1);

  paths->paths[0] = create_basic_data_bundle_file();

  worker_data_read(paths, worker_data);

  batch_data    = batch_data_new(worker_data, 2);
  network_state = network_state_basic();
  expected      = correction_expected_basic_bundle_file();

  correction = gradient_descent(
    worker_data,
    batch_data,
    network_state,
    0
  );

  assert(correction->layers == 3); /* LCOV_EXCL_BR_LINE */

  for (int32_t layer = 0; layer < correction->layers; layer += 1) {
    assert(matrix_equal(correction->biases[layer],  expected->biases[layer]) ); /* LCOV_EXCL_BR_LINE */
    assert(matrix_equal(correction->weights[layer], expected->weights[layer])); /* LCOV_EXCL_BR_LINE */
  }

  batch_data_free(&batch_data);
  correction_free(&correction);
  network_state_free(&network_state);
  bundle_paths_free(&paths);
  worker_data_free(&worker_data);
}
Ejemplo n.º 2
0
static char * test_create_initial_board_from_file_with_empty_lines() {

  Matrix* board;
  Matrix* expected_matrix;

  char* filename;
  filename = "test_island/test_island_empty_lines.txt";

  expected_matrix = matrix_create(2, 2);
  /*
  1 2
  3 4
  */
  matrix_set(expected_matrix, 0, 0, 1);
  matrix_set(expected_matrix, 0, 1, 2);

  matrix_set(expected_matrix, 1, 0, 3);
  matrix_set(expected_matrix, 1, 1, 4);

  board = create_initial_board_from_file(filename);

  /*
  print_board("board:  ", board);
  print_board("expected:  ", expected_matrix);
  */

  mu_assert("error, expected_matrix != board_from_file", matrix_equal(board, expected_matrix));
  return 0;
}
Ejemplo n.º 3
0
static void test_forward_for_activity() {
  NetworkState *network_state = network_state_basic();
  Matrix        sample        = data_sample_basic();
  Activation   *activity      = forward_for_activity(network_state, sample);

  assert(activity->layers == network_state->layers); /* LCOV_EXCL_BR_LINE */

  float layer_1_input[5] = {1, 3, 31, 38, 45};
  float layer_2_input[4] = {1, 2, 371, 486};
  float layer_3_input[4] = {1, 2, 1830, 2688};

  assert(activity->input[0] == NULL); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->input[1], layer_1_input)); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->input[2], layer_2_input)); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->input[3], layer_3_input)); /* LCOV_EXCL_BR_LINE */

  float layer_0_output[5] = {1, 3, 1, 2, 3};
  float layer_1_output[5] = {1, 3, 31, 38, 45};
  float layer_2_output[4] = {1, 2, 371, 486};
  float layer_3_output[4] = {1, 2, 1830, 2688};

  assert(matrix_equal(activity->output[0], layer_0_output)); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->output[1], layer_1_output)); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->output[2], layer_2_output)); /* LCOV_EXCL_BR_LINE */
  assert(matrix_equal(activity->output[3], layer_3_output)); /* LCOV_EXCL_BR_LINE */

  assert(activity->mask[0] == NULL); /* LCOV_EXCL_BR_LINE */
  assert(activity->mask[1] == NULL); /* LCOV_EXCL_BR_LINE */
  assert(activity->mask[2] == NULL); /* LCOV_EXCL_BR_LINE */
  assert(activity->mask[3] == NULL); /* LCOV_EXCL_BR_LINE */

  activation_free(&activity);
  matrix_free(&sample);
  network_state_free(&network_state);
}
Ejemplo n.º 4
0
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 );
}
Ejemplo n.º 5
0
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 );
}
Ejemplo n.º 6
0
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 );
}
Ejemplo n.º 7
0
/*
Returns 1 if the matrix is transitive. Uses the 
*/
int is_transitive(int *matrix, int size){
	//create the product matrix
	int product[size];
	//wipe whatever garbage is in there
	int i=0;
	for(i=0; i<size; i++){
		product[i] = 0;
	}
	//square the matrix and store it in the product matrix
	multiply(matrix, matrix, product, size);
	//make sure every 1 in the product is matched with a 1 in the matrix
	int test_matrix[size];
	for(i=0; i<size; i++){
		test_matrix[i] = matrix[i] | product[i];
	}
	//if the test matrix is the same as the original then it's transitive
	if(matrix_equal(matrix, test_matrix, size)){
		return 1;
	}
	return 0;
}
Ejemplo n.º 8
0
void testS( ert_test_context_type * test_context ) {
  {
    enkf_main_type * enkf_main = ert_test_context_get_main( test_context );
    enkf_obs_type * enkf_obs = enkf_main_get_obs( enkf_main );
    enkf_fs_type * fs = enkf_main_get_fs( enkf_main );
    int_vector_type * active_list = int_vector_alloc(0,0);
    obs_data_type * obs_data = obs_data_alloc(1.0);
    local_obsdata_type * obs_set = local_obsdata_alloc( "KEY" );
    bool_vector_type * ens_mask;
    meas_data_type * meas_data;


    for (int i= 0; i < enkf_main_get_ensemble_size( enkf_main); i++)
      int_vector_append( active_list , i );
    ens_mask = int_vector_alloc_mask( active_list);

    obs_data = obs_data_alloc(1.0);
    meas_data = meas_data_alloc( ens_mask );

    enkf_obs_add_local_nodes_with_data( enkf_obs  , obs_set , fs , ens_mask );
    enkf_obs_get_obs_and_measure_data( enkf_obs , fs , obs_set, FORECAST , active_list , meas_data , obs_data);

    {
      FILE * stream = util_fopen("analysis/Smatrix" , "r");
      matrix_type * S = meas_data_allocS( meas_data );
      matrix_type * S0 = matrix_fread_alloc( stream );

      test_assert_true( matrix_equal( S0 , S ));

      matrix_free( S );
      matrix_free( S0 );
      fclose( stream );
    }
    int_vector_free( active_list );
    meas_data_free( meas_data );
    obs_data_free( obs_data );
    local_obsdata_free( obs_set );
    bool_vector_free( ens_mask );
  }
}
Ejemplo n.º 9
0
int main(int argc, char** argv)
{
    int **m, *s;
    int n, i, min, max;

    int * op_order;
    int * strassen_res, * naive_res;

    double t;

    if (argc<4)
    {
	printf("Too few arguments\n");
	return 1;
    }
    
    // 1er argument : nombre de matrices
    sscanf(argv[1],"%d",&n);

    // 2e argument : min dimensions
    sscanf(argv[2], "%d", &min);

    // 3e arg : max dimensions
    sscanf(argv[3], "%d", &max);

    
    srand(n*min*max);
    m = randmatrices(n, &s, min, max);

#if PRINTDEMO
    if (n<10 && max<10)
	for (i=0 ; i<n ; i++)
            print_matrix(m[i],s[i],s[i+1]);
#endif

    t = clock();
    op_order = get_optimal_product (n, s);
    printf("Parenthesization time : %.3fs\n", (clock()-t)/CLOCKS_PER_SEC);

    print_paren(op_order, s, n - 1);


    t = clock();
    strassen_res = orderedmult (m, s, op_order, n);
    printf("Strassen time : %.3fs\n", (clock()-t)/CLOCKS_PER_SEC);


    // The following code is used only for the demo program (it computes the 
    // product and also checks if the product seems okay)
#ifdef CHECK_NAIVE
    t = clock();
    naive_res = naive_product (m, s, n);
    printf("Naive product time : %.3fs\n", (clock()-t)/CLOCKS_PER_SEC);

    if (!matrix_equal (strassen_res, naive_res, s[0], s[n]))
    {
      printf("Error : matrices are not equal\n");
      return 1;
    }
    else
      printf("Ok, result checked\n");
#endif

#if PRINTDEMO
    if (max<20)
	print_matrix(strassen_res,s[0],s[n]);
#endif
    // No need to free : we're exiting, the OS will get all the memory back...
    free(strassen_res);
    return 0;    
}
Ejemplo n.º 10
0
int main(int argc, char *argv[])
{
	struct thread_data *threads;
	struct thread_data *thread;
	int i, ret, ch;

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0)
		{
			usage_error(argv[0]);
		}
		init_program_parameter(argc, argv);
	}

	program_parameter(argv[0]);

	create_matrix(&matrix_a);
	create_matrix(&matrix_b);
	create_matrix(&matrix_c);
	create_matrix(&matrix_d);
	random_matrix(matrix_a);
	random_matrix(matrix_b);

	nonmal_matrix_multipy(matrix_a, matrix_b, matrix_d);

	threads = (struct thread_data *)malloc(pthread_max * sizeof(struct thread_data));
	if (threads == NULL)
	{
		unix_error("malloc threads failed");
	}

	cpu_online = sysconf(_SC_NPROCESSORS_CONF);

	for(i = 0; i < pthread_max; i++)
	{
		thread = threads + i;
		thread->index = i;
		if ((ret = pthread_create(&thread->thread_id, NULL, thread_func, thread)) != 0)
		{
			posix_error(ret, "pthread_create failed");
		}
	}

	for(i = 0; i < pthread_max; i++)
	{
		thread = threads + i;
		if ((ret = pthread_join(thread->thread_id, NULL)) != 0)
		{
			posix_error(ret, "pthread_join failed");
		}
	}

	if (matrix_equal(matrix_c, matrix_d) == 0)
	{
		unix_error("runtime error");
	}
	if (dump)
	{
		dump_matrix("matrix A", matrix_a);
		dump_matrix("matrix B", matrix_b);
		dump_matrix("matrix C", matrix_c);
		dump_matrix("matrix D", matrix_d);
	}
	statistics(threads);

	free_matrix(matrix_a);
	free_matrix(matrix_b);
	free_matrix(matrix_c);
	free_matrix(matrix_d);
	free(threads);
	return 0;
}
Ejemplo n.º 11
0
int main (int argn, char** argv)
{
#if 1

  int m, n, _n, o;
  int * A, * B, * C, * D;
  double t;
  
  if (NULL == (A = read_matrix(&m,&n)))
  {
      printf("Could not read 1st matrix.\n");
      return 1;
  }

  if (NULL == (B = read_matrix(&_n,&o)))
  {
      printf("Could not read 2nd matrix.\n");
      return 1;
  }
  else if (_n!=n)
  {
      printf("Matrix sizes not compatible.\n");
      return 1;
  }
#if PRINT
  print_matrix(A, m, n);
  print_matrix(B, n, o);
  printf("Strassen...\n");
#endif

  t=clock();
  if (NULL == (C = strassen(A,B,m,n,o)))
  {
      printf("Multiplication failed.\n");
      return 1;
  }

#if PRINT
  printf("Time : %.3fs\n",(clock()-t)/CLOCKS_PER_SEC);
  print_matrix(C, m, o);
#else
  printf((MULT_NAIVE)?"%.4f,":"%.4f\n",(clock()-t)/CLOCKS_PER_SEC);
#endif

#if PRINT
  printf("Naive mult...\n");
#endif

#if MULT_NAIVE==1
  t=clock();
  if (NULL == (D = naive_mult(A,B,m,n,o)))
  { 
      printf("Multiplication failed.\n");
      return 2;
  }

#if PRINT
  printf("Time : %.3fs\n",(clock()-t)/CLOCKS_PER_SEC);
  print_matrix(D, m, o);

  if (matrix_equal(C,D,m,o))
      printf("1 : Okay\n");
  else printf("0 : Not Okay\n");
#else
  printf("%.4f\n",(clock()-t)/CLOCKS_PER_SEC);
#endif
#endif
  free(A);
  free(B);
  free(C);
  free(D);
  A=B=C=D=0;
#endif

  return 0;
}