int main( int argc, char *argv[] ) { try { wink::rand32_kiss g; g.seed( uint32_t(time(NULL)) ); const size_t Nx = 10 + g.less_than(20); const size_t Ny = 10 + g.less_than(20); double *M = new double [Nx+Ny]; double *X = M; double *Y = M + Nx; g.fill_array(0,1,X,Nx); save_array(X, Nx, "x.dat"); g.fill_array(0,1,Y,Ny); save_array(Y, Ny, "y.dat"); const size_t nc = wink::coincidences(X, Nx, Y, Ny, 0.01); std::cerr << "NC=" << nc << std::endl; delete []M; return 0; } catch(...) { std::cerr << "Error detected" << std::endl; } return 1; }
int test3(int argc, char * argv[]) { if ( argc != 2 ) { std::cout << "error." << std::endl; std::cout << "./run <n>" << std::endl; return 1; } const int n = std::atoi(argv[1]); //---------------------------------------------- // define space domain //---------------------------------------------- const int D = 2; const real l = 1.0; const int hn = 3; //---------------------------------------------- // define mesh //---------------------------------------------- const Mesh mesh( hn, l, n, l, n ); real * u = new real[mesh.size()]; real * v = new real[mesh.size()]; real * p = new real[mesh.size()]; uniform_value( u, 2.0, mesh ); uniform_value( v, 2.0, mesh ); uniform_value( p, 0.0, mesh ); const std::string u_init_filename( "u_init.dat" ); const std::string u_position( "staggered_x" ); save_array( D, u, u_init_filename, u_position, mesh ); const std::string v_filename( "v.dat" ); const std::string v_position( "staggered_y" ); save_array( D, v, v_filename, v_position, mesh ); const std::string p_filename( "p.dat" ); const std::string p_position( "cell_center" ); save_array( D, p, p_filename, p_position, mesh ); //boundary_update_u_v( u, v, mesh ); boundary_update_only_u(u, mesh); boundary_update_only_v(v, mesh); const std::string u_filename( "u.dat" ); save_array( D, u, u_filename, u_position, mesh ); const std::string v_init_filename( "v_init.dat" ); save_array( D, v, v_init_filename, v_position, mesh ); delete[] u; delete[] v; delete[] p; return 0; }
int test1(int argc, char* argv[]) { if ( argc != 2 ) { std::cout << "error." << std::endl; std::cout << "./run <n>" << std::endl; return 1; } const int n = std::atoi(argv[1]); //---------------------------------------------- // define space domain //---------------------------------------------- const int D = 2; const real l = 1.0; const int hn = 3; //---------------------------------------------- // define mesh //---------------------------------------------- const Mesh mesh( hn, l, n, l, n ); mesh.print_mesh_data(); real * u = new real[mesh.size()]; uniform_value( u, -100.0, mesh ); const std::string u_filename( "u.dat" ); const std::string u_position( "cell_center" ); save_array( D, u, u_filename, u_position, mesh ); delete[] u; return 0; }
int test2(int argc, char * argv[]) { if ( argc != 2 ) { std::cout << "error." << std::endl; std::cout << "./run <n>" << std::endl; return 1; } const int n = std::atoi(argv[1]); //---------------------------------------------- // define space domain //---------------------------------------------- const int D = 2; const real l = 1.0; const int hn = 3; //---------------------------------------------- // define mesh //---------------------------------------------- const Mesh mesh( hn, l, n, l, n ); //mesh.print_mesh_data(); real * phi = new real[mesh.size()]; uniform_value( phi, 200.0, mesh ); const int k = mesh.hn() ; for ( int j = mesh.hn() ; j < mesh.hnf(1) ; j++ ) { for ( int i = mesh.hn() ; i < mesh.hnf(0) ; i++ ) { const int id = mesh.id(i,j,k); phi[id] = 100; }} const std::string u_init_filename( "phi_init.dat" ); const std::string u_position( "cell_center" ); save_array( D, phi, u_init_filename, u_position, mesh ); boundary_update_phi( phi, mesh ); const std::string u_filename( "phi.dat" ); save_array( D, phi, u_filename, u_position, mesh ); delete[] phi; return 0; }
void save_matrix(float *arr, char *s, int dim0, int dim1) { int dim[2]; dim[0] = dim0; dim[1] = dim1; save_array(arr, s, 2, dim); }
static void save_one(struct savebuf *f, struct svalue *v) { static char buf[48]; static int depth = 0; if (++depth > MAX_DEPTH) { free(f->buf); depth = 0; error("Too deep recursion\n"); } switch(v->type) { case T_FLOAT: (void)sprintf(buf,"#%a#", v->u.real); add_strbuf(f, buf); break; case T_NUMBER: (void)sprintf(buf, "%lld", v->u.number); add_strbuf(f, buf); break; case T_STRING: save_string(f, v->u.string); break; case T_POINTER: save_array(f, v->u.vec); break; case T_MAPPING: save_mapping(f, v->u.map); break; case T_OBJECT: (void)sprintf(buf, "$%d@", v->u.ob->created); add_strbuf(f, buf); add_strbuf(f, v->u.ob->name); add_strbuf(f, "$"); break; #if 0 case T_FUNCTION: add_strbuf(f, "&FUNCTION&"); /* XXX function */ break; #endif default: add_strbuf(f, "0"); break; } depth--; }
static gboolean export_numpy (GeglOperation *operation, GeglBuffer *input, const GeglRectangle *result, GOutputStream *stream) { const Babl *input_format, *output_format; gint nb_components; input_format = gegl_buffer_get_format (input); nb_components = babl_format_get_n_components (input_format); if (nb_components >= 3) { output_format = babl_format ("RGB float"); nb_components = 3; } else { output_format = babl_format ("Y float"); nb_components = 1; } return !save_array (stream, input, result, output_format); }
void save(serialization::array<T> const& x) { save_array(x,0u); }
int test(int argc, char* argv[]) { if ( argc != 3 ) { std::cout << "error." << std::endl; std::cout << "./run <n> <endtimestep>" << std::endl; return 1; } const int n = std::atoi(argv[1]); //---------------------------------------------- // define space domain //---------------------------------------------- const int D = 2; const real l = 1.0; const int hn = 3; //const int hn = 2; //---------------------------------------------- // define mesh //---------------------------------------------- const Mesh mesh( hn, l, n, l, n ); //mesh.print_mesh_data(); //---------------------------------------------- // define time domain //---------------------------------------------- const real dt = 0.1 * mesh.delta(0) / 1.0; //const real dt = 1.0e-5; const real endtime = 200 * 1.0 / 1.0; const int endstep = static_cast<int>(endtime / dt); //const int endstep = std::atoi(argv[2]); real * u = new real[mesh.size()]; real * v = new real[mesh.size()]; real * p = new real[mesh.size()]; real * nu = new real[mesh.size()]; real * nv = new real[mesh.size()]; uniform_value( u, 0.0, mesh ); uniform_value( v, 0.0, mesh ); uniform_value( nu, 0.0, mesh ); uniform_value( nv, 0.0, mesh ); uniform_value( p, 0.0, mesh ); boundary_update_u_v( u, v, mesh ); for ( int timestep = 0 ; timestep < endstep ; timestep++ ) { const Iter iter = time_marching_RK1_cavity( nu, nv, p, u, v, dt, mesh ); swap( &nu, &u ); swap( &nv, &v ); //const Iter iter = time_marching_RK2_cavity( &u, &v, &p, dt, mesh ); if ( timestep % 100 == 0 ) { real * const div = new real[mesh.size()]; uniform_value( div, 0.0, mesh ); calc_div( div, u, v, mesh ); const std::string div_filename( "div.dat" ); const std::string div_position( "cell_center" ); //save_array( D, div, div_filename, div_position, mesh ); abs_array( div, mesh ); const real max_abs_div_value = max_from_array( div, mesh ); delete[] div; std::cout << timestep / static_cast<real>(endstep) * 100 << " %"; std::cout << "\t timestep = " << timestep; std::cout << "\titer = " << iter.get_iter() << "\terr = " << iter.get_error(); std::cout << "\tdiv = " << std::scientific << std::showpos << max_abs_div_value << std::endl; } } const std::string u_filename( "u.dat" ); const std::string u_position( "staggered_x" ); save_array( D, u, u_filename, u_position, mesh ); const std::string v_filename( "v.dat" ); const std::string v_position( "staggered_y" ); save_array( D, v, v_filename, v_position, mesh ); const std::string p_filename( "p.dat" ); const std::string p_position( "cell_center" ); save_array( D, p, p_filename, p_position, mesh ); output_result( "result_u.dat", "result_v.dat", u, v, mesh ); delete[] u; delete[] v; delete[] p; delete[] nu; delete[] nv; return 0; }
void EditableMap::save() throw (Exception) { char buffer[256]; /* drop header informations */ int height = atoi(get_value("height").c_str()); for (int i = 0; i < height; i++) { sprintf(buffer, "tiles%d", i); set_value(buffer, ""); sprintf(buffer, "decoration%d", i); set_value(buffer, ""); } /* drop objects */ int cnt_objects = atoi(get_value("objects").c_str()); for (int i = 0; i < cnt_objects; i++) { sprintf(buffer, "object_name%d", i); set_value(buffer, ""); sprintf(buffer, "object_x%d", i); set_value(buffer, ""); sprintf(buffer, "object_y%d", i); set_value(buffer, ""); } /* drop light sources */ int cnt_lights = atoi(get_value("lights").c_str()); for (int i = 0; i < cnt_lights; i++) { sprintf(buffer, "light_x%d", i); set_value(buffer, ""); sprintf(buffer, "light_y%d", i); set_value(buffer, ""); sprintf(buffer, "light_radius%d", i); set_value(buffer, ""); } int width = get_width(); height = get_height(); set_value("width", width); set_value("height", height); save_array("tiles", get_map(), width, height); save_array("decoration", get_decoration(), width, height); set_value("game_play_type", static_cast<int>(game_play_type)); set_value("frog_spawn_init", frog_spawn_init); cnt_objects = static_cast<int>(objects.size()); set_value("objects", cnt_objects); for (int i = 0; i < cnt_objects; i++) { EditableObject *eobj = objects[i]; sprintf(buffer, "object_name%d", i); set_value(buffer, eobj->object->get_name()); sprintf(buffer, "object_x%d", i); set_value(buffer, eobj->x); sprintf(buffer, "object_y%d", i); set_value(buffer, eobj->y); } cnt_lights = static_cast<int>(lights.size()); set_value("lights", cnt_lights); for (int i = 0; i < cnt_lights; i++) { EditableLight *elgt = lights[i]; sprintf(buffer, "light_x%d", i); set_value(buffer, elgt->x); sprintf(buffer, "light_y%d", i); set_value(buffer, elgt->y); sprintf(buffer, "light_radius%d", i); set_value(buffer, elgt->radius); } std::string save_dir = get_home_directory() + dir_separator + UserDirectory; create_directory("maps", save_dir); KeyValue::save(save_dir + dir_separator + "maps" + dir_separator + get_name() + ".map"); }
/** * @brief main method of the program. * @param argc The number of the arguments of the program. * @param argv The arguments of the program. * @return the exit status of the process. */ int main(int argc, char* argv[]) { (void) setup_signal(); /** Declare the variables */ char* username; char* password; int opt; int binr = 0; int binl = 0; struct data *shared; int shmfd; sem_t *sem1; sem_t *sem2; sem_t *sem3; char buffer[MAX_DATA+1]; int command = EMPTY; int current_flag = EMPTY; /** Check the optional arguments */ while ((opt = getopt(argc, argv, "rl")) != -1) { switch(opt) { case 'r': binr = 1; break; case 'l': binl = 1; break; case '?': (void) usage(argv[0]); default: (void) usage(argv[0]); } } /** Check the number of the arguments */ if(!(argc == 4 && (binr^binl))) { (void) usage(argv[0]); } username = argv[2]; password = argv[3]; /** Open the semaphores */ sem1 = sem_open(SEM_1, 0); sem2 = sem_open(SEM_2, 0); sem3 = sem_open(SEM_3, 0); if (sem1 == SEM_FAILED || sem2 == SEM_FAILED || sem3 == SEM_FAILED) { (void) on_error("Problem while executing sem_open(3)."); } /** Create a share memory */ shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, PERMISSION); if(shmfd == -1) { (void) on_error("Problem while executing shm_open(3)."); } /** Map the data to be shared */ shared = mmap(NULL, sizeof *shared, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); if (shared == MAP_FAILED) { (void) on_error("Problem while executing mmap(2)."); } /** Close the shared memory file descriptor */ if (close(shmfd) == -1) { (void) on_error("Problem while executing close(2)."); } /** critical section */ if (sem_wait(sem3) == -1) { if (errno != EINTR) (void) on_error("Problem while executing sem_wait(3)."); } if(binr) { shared->flag = REGISTER; } else if(binl) { shared->flag = LOGIN; } /** Save the arrays in the shared memory */ (void) save_array(shared->username, username); (void) save_array(shared->password, password); if (sem_post(sem1) == -1) { (void) on_error("Problem while executing sem_post(3)."); } if (sem_wait(sem2) == -1) { if (errno != EINTR) (void) on_error("Problem while executing sem_wait(3)."); } current_flag = shared->flag; if (sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } if(binr && (current_flag == ERROR)) { (void) on_error("User with this name and password already registered."); } else if (binl && (current_flag == ERROR)) { (void) on_error("User with this name and password does not exist."); } while (command != 3 && !want_quit) { if (sem_wait(sem3) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } if (shared->flag == SERVER_ERROR) { break; } if (sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } (void) print_options(); if (read(STDIN_FILENO, buffer, MAX_DATA) == -1) { (void) on_error("Problem while executing read(2)."); } buffer[MAX_DATA] = '\0'; command = (int)strtol(buffer, NULL, 10); switch (command) { case 1: (void) fprintf(stdout, "\nWrite a new secret:\n"); char buffer[MAX_DATA+1]; if (read(STDIN_FILENO, buffer, MAX_DATA) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing read(2)."); } buffer[MAX_DATA] = '\0'; if (sem_wait(sem3) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } (void) strcpy(shared->username, username); (void) strcpy(shared->password, password); (void) strcpy(shared->secret, buffer); shared->flag = WRITE_SECRET; if (sem_post(sem1) == -1 || sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } if (sem_wait(sem2) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } break; case 2: (void) fprintf(stdout, "\nReading the secret:\n"); if (sem_wait(sem3) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } (void) strcpy(shared->username, username); (void) strcpy(shared->password, password); shared->flag = READ_SECRET; if (sem_post(sem1) == -1) { (void) on_error("Problem while executing sem_post(3)."); } if (sem_wait(sem2) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } int i = 0; if (shared->secret[i] == '\0') { (void) fprintf(stdout, "No secret set."); } while (shared->secret[i] != '\0') { (void) fprintf(stdout, "%c", shared->secret[i]); i++; } (void) fprintf(stdout, "\n\n"); if (sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } break; case 3: if (sem_wait(sem3) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } (void) strcpy(shared->username, username); (void) strcpy(shared->password, password); shared->flag = LOGOUT; if (sem_post(sem1) == -1 || sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } (void) fprintf(stdout, "\nLogging out...\n\n"); if (sem_wait(sem2) == -1) { if (errno == EINTR) continue; (void) on_error("Problem while executing sem_wait(3)."); } break; default: (void) fprintf(stdout, "\nInvalid command!\n\n"); command = EMPTY; break; } } /** send error code to the server */ if (want_quit == 1) { if (sem_wait(sem3) == -1) { (void) on_error("Problem while executing sem_wait(3)."); } shared->flag = ERROR; if (sem_post(sem3) == -1) { (void) on_error("Problem while executing sem_post(3)."); } } /** critical section out */ /** Unmap the data */ if (munmap(shared, sizeof *shared) == -1) { (void) on_error("Problem while executing munmap(2)."); } /** Clse the semaphores */ if (sem_close(sem1) == -1) { (void) on_error("Problem while executing sem_close(3)."); } if (sem_close(sem2) == -1) { (void) on_error("Problem while executing sem_close(3)."); } if (sem_close(sem3) == -1) { (void) on_error("Problem while executing sem_close(3)."); } return EXIT_SUCCESS; }
void main() { float sarray[2][2][2]; /* for saving with save_vector, save_matrix, save_array */ float ***darray; /* for saving with save_vector, save_matrix2 */ float sloadarray[2][2][2]; /* for loading with load_in_vector, load_in_matrix, load_in_array */ /* float ***dloadarray; */ /* for loading with load_vector, load_matrix */ int i,j; /* int dim[3]; */ int save_dim[3] = {2, 2, 2}; darray = (float ***)malloc(save_dim[0]*sizeof(float**)); for(i=0;i<save_dim[0];i++){ darray[i] = (float**)malloc(save_dim[1]*sizeof(float*)); for(j=0;j<save_dim[1];j++) darray[i][j] = (float*)malloc(save_dim[2]*sizeof(float)); } printf("toy 3-dim array:"); for(i=0; i<save_dim[0]; i++){ printf("\n"); for( j=0; j<save_dim[1]; j++){ darray[i][j][0] = 1 + j - i + (i+0.6) * 0.12342346; sarray[i][j][0] = 1 + j - i + (i+0.6) * 0.12342346; darray[i][j][1] = j - i + (i+0.6) * 0.1234; sarray[i][j][1] = j - i + (i+0.6) * 0.1234; printf("%f %f ; ", darray[i][j][0], darray[i][j][1]); } } printf("\n\n"); save_array ((float*)sarray, "sbinary.mat", 3, save_dim); save_packed_array((float*)sarray, "spacked.mat", 3, save_dim); save_ascii_array ((float*)sarray, "sascii.mat", 3, save_dim); printf("saved 6 arrays\n"); load_in_array((float*)sloadarray, "sbinary.mat", 3, save_dim); for(i=0; i<save_dim[0]; i++){ printf("\n"); for( j=0; j<save_dim[1]; j++) printf("%f %f ; ", sloadarray[i][j][0], sloadarray[i][j][1]); } printf("\n"); load_in_array((float*)sloadarray, "sascii.mat", 3, save_dim); for(i=0; i<save_dim[0]; i++){ printf("\n"); for( j=0; j<save_dim[1]; j++) printf("%f %f ; ", sloadarray[i][j][0], sloadarray[i][j][1]); } printf("\n"); load_in_array((float*)sloadarray, "spacked.mat", 3, save_dim); for(i=0; i<save_dim[0]; i++){ printf("\n"); for( j=0; j<save_dim[1]; j++) printf("%f %f ; ", sloadarray[i][j][0], sloadarray[i][j][1]); } printf("\n\n"); printf("And now an example of what you must not do: save dynamically\n"); printf("allocated general arrays with <save_array> - this will just save\n"); printf("a contingent block of data starting from the pointer to the array."); printf("\nReloading then gives:\n"); save_array((float*)darray, "dbinary.mat", 3, save_dim); load_in_array((float*)sloadarray, "dpacked.mat", 3, save_dim); for(i=0; i<save_dim[0]; i++){ printf("\n"); for( j=0; j<save_dim[1]; j++) printf("%f %f ; ", sloadarray[i][j][0], sloadarray[i][j][1]); } printf("\n"); printf("If you want, write a function <save_array2> that does the job.\n\n"); printf("In addition, one could write a function that loads a general\n"); printf("array and automatically allocates storage for it.\n"); printf("An easy workaround would be to load the array into a vector,\n"); printf("and return the dimensions.\n"); }
void save_vector(float *arr, char *s, int length) { save_array(arr, s, 1, &length); }