bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName, double &dfResult, bool bReportError ) { hid_t hAttr = H5Aopen_name( loc_id, pszAttrName ); dfResult = 0.0; if( hAttr < 0 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, not found.", pszAttrName ); return false; } hid_t hAttrTypeID = H5Aget_type( hAttr ); hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); /* -------------------------------------------------------------------- */ /* Confirm that we have a single element value. */ /* -------------------------------------------------------------------- */ hid_t hAttrSpace = H5Aget_space( hAttr ); hsize_t anSize[64]; int nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, anSize, NULL ); int i, nAttrElements = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElements *= (int) anSize[i]; } if( nAttrElements != 1 ) { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attempt to read attribute %s failed, count=%d, not 1.", pszAttrName, nAttrElements ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return false; } /* -------------------------------------------------------------------- */ /* Read the value. */ /* -------------------------------------------------------------------- */ void *buf = (void *)CPLMalloc( H5Tget_size( hAttrNativeType )); H5Aread( hAttr, hAttrNativeType, buf ); /* -------------------------------------------------------------------- */ /* Translate to double. */ /* -------------------------------------------------------------------- */ if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) dfResult = *((int *) buf); else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) dfResult = *((float *) buf); else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) dfResult = *((double *) buf); else { if( bReportError ) CPLError( CE_Failure, CPLE_AppDefined, "Attribute %s of unsupported type for conversion to double.", pszAttrName ); CPLFree( buf ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return false; } CPLFree( buf ); H5Sclose( hAttrSpace ); H5Tclose( hAttrNativeType ); H5Tclose( hAttrTypeID ); H5Aclose( hAttr ); return true; }
void op_write_hdf5(char const * file_name) { printf("Writing to %s\n",file_name); //declare timers double cpu_t1, cpu_t2, wall_t1, wall_t2; double time; double max_time; op_timers(&cpu_t1, &wall_t1); //timer start for hdf5 file write //create new communicator int my_rank, comm_size; MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD); MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank); MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size); //MPI variables MPI_Info info = MPI_INFO_NULL; //HDF5 APIs definitions hid_t file_id; //file identifier hid_t plist_id; //property list identifier hid_t dset_id = 0; //dataset identifier hid_t dataspace; //data space identifier hid_t memspace; //memory space identifier hsize_t dimsf[2]; // dataset dimensions hsize_t count[2]; //hyperslab selection parameters hsize_t offset[2]; //Set up file access property list with parallel I/O access plist_id = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info); //Create a new file collectively and release property list identifier. file_id = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id); H5Pclose(plist_id); /*loop over all the op_sets and write them to file*/ for(int s=0; s<OP_set_index; s++) { op_set set=OP_set_list[s]; //Create the dataspace for the dataset. hsize_t dimsf_set[] = {1}; dataspace = H5Screate_simple(1, dimsf_set, NULL); //Create the dataset with default properties and close dataspace. dset_id = H5Dcreate(file_id, set->name, H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); //Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE); int size = 0; int* sizes = (int *)xmalloc(sizeof(int)*comm_size); MPI_Allgather(&set->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD); for(int i = 0; i<comm_size; i++)size = size + sizes[i]; //write data H5Dwrite(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, plist_id, &size); H5Sclose(dataspace); H5Pclose(plist_id); H5Dclose(dset_id); } /*loop over all the op_maps and write them to file*/ for(int m=0; m<OP_map_index; m++) { op_map map=OP_map_list[m]; //find total size of map int* sizes = (int *)xmalloc(sizeof(int)*comm_size); int g_size = 0; MPI_Allgather(&map->from->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD); for(int i = 0; i<comm_size; i++)g_size = g_size + sizes[i]; //Create the dataspace for the dataset. dimsf[0] = g_size; dimsf[1] = map->dim; dataspace = H5Screate_simple(2, dimsf, NULL); //Create the dataset with default properties and close dataspace. if(sizeof(map->map[0]) == sizeof(int)) dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else if(sizeof(map->map[0]) == sizeof(long)) dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_LONG, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else if(sizeof(map->map[0]) == sizeof(long long)) dset_id = H5Dcreate(file_id, map->name, H5T_NATIVE_LLONG, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Sclose(dataspace); //Each process defines dataset in memory and writes it to a hyperslab //in the file. int disp = 0; for(int i = 0; i<my_rank; i++)disp = disp + sizes[i]; count[0] = map->from->size; count[1] = dimsf[1]; offset[0] = disp; offset[1] = 0; memspace = H5Screate_simple(2, count, NULL); //Select hyperslab in the file. dataspace = H5Dget_space(dset_id); H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); //Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE); //write data if(sizeof(map->map[0]) == sizeof(int)) H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, map->map); else if(sizeof(map->map[0]) == sizeof(long)) H5Dwrite(dset_id, H5T_NATIVE_LONG, memspace, dataspace, plist_id, map->map); else if(sizeof(map->map[0]) == sizeof(long long)) H5Dwrite(dset_id, H5T_NATIVE_LLONG, memspace, dataspace, plist_id, map->map); H5Pclose(plist_id); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dset_id); free(sizes); /*attach attributes to map*/ //open existing data set dset_id = H5Dopen(file_id, map->name, H5P_DEFAULT); //create the data space for the attribute hsize_t dims = 1; dataspace = H5Screate_simple(1, &dims, NULL); //Create an int attribute - size hid_t attribute = H5Acreate(dset_id, "size", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT); //Write the attribute data. H5Awrite(attribute, H5T_NATIVE_INT, &g_size); //Close the attribute. H5Aclose(attribute); //Create an int attribute - dimension attribute = H5Acreate(dset_id, "dim", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT); //Write the attribute data. H5Awrite(attribute, H5T_NATIVE_INT, &map->dim); //Close the attribute. H5Aclose(attribute); H5Sclose(dataspace); //Create an string attribute - type dataspace= H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 10); attribute = H5Acreate(dset_id, "type", atype, dataspace, H5P_DEFAULT, H5P_DEFAULT); if(sizeof(map->map[0]) == sizeof(int)) H5Awrite(attribute, atype, "int"); if(sizeof(map->map[0]) == sizeof(long)) H5Awrite(attribute, atype, "long"); if(sizeof(map->map[0]) == sizeof(long long)) H5Awrite(attribute, atype, "long long"); H5Aclose(attribute); //Close the dataspace H5Sclose(dataspace); //Close to the dataset. H5Dclose(dset_id); } /*loop over all the op_dats and write them to file*/ for(int d=0; d<OP_dat_index; d++) { op_dat dat=OP_dat_list[d]; //find total size of map int* sizes = (int *)xmalloc(sizeof(int)*comm_size); int g_size = 0; MPI_Allgather(&dat->set->size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD); for(int i = 0; i<comm_size; i++)g_size = g_size + sizes[i]; //Create the dataspace for the dataset. dimsf[0] = g_size; dimsf[1] = dat->dim; dataspace = H5Screate_simple(2, dimsf, NULL); //Create the dataset with default properties and close dataspace. if(strcmp(dat->type,"double")==0) dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_DOUBLE, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else if(strcmp(dat->type,"float")==0) dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_FLOAT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else if(strcmp(dat->type,"int")==0) dset_id = H5Dcreate(file_id, dat->name, H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else printf("Unknown type\n"); H5Sclose(dataspace); //Each process defines dataset in memory and writes it to a hyperslab //in the file. int disp = 0; for(int i = 0; i<my_rank; i++)disp = disp + sizes[i]; count[0] = dat->set->size; count[1] = dimsf[1]; offset[0] = disp; offset[1] = 0; memspace = H5Screate_simple(2, count, NULL); //Select hyperslab in the file. dataspace = H5Dget_space(dset_id); H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); //Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE); //write data if(strcmp(dat->type,"double") == 0) H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, dataspace, plist_id, dat->data); else if(strcmp(dat->type,"float") == 0) H5Dwrite(dset_id, H5T_NATIVE_FLOAT, memspace, dataspace, plist_id, dat->data); else if(strcmp(dat->type,"int") == 0) H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, dat->data); else printf("Unknown type\n"); H5Pclose(plist_id); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dset_id); free(sizes); /*attach attributes to dat*/ //open existing data set dset_id = H5Dopen(file_id, dat->name, H5P_DEFAULT); //create the data space for the attribute hsize_t dims = 1; dataspace = H5Screate_simple(1, &dims, NULL); //Create an int attribute - size hid_t attribute = H5Acreate(dset_id, "size", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT); //Write the attribute data. H5Awrite(attribute, H5T_NATIVE_INT, &dat->size); //Close the attribute. H5Aclose(attribute); //Create an int attribute - dimension attribute = H5Acreate(dset_id, "dim", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT); //Write the attribute data. H5Awrite(attribute, H5T_NATIVE_INT, &dat->dim); H5Aclose(attribute); H5Sclose(dataspace); //Create an string attribute - type dataspace= H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 10); attribute = H5Acreate(dset_id, "type", atype, dataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attribute, atype, dat->type); H5Aclose(attribute); //Close the dataspace. H5Sclose(dataspace); H5Dclose(dset_id); } H5Fclose(file_id); op_timers(&cpu_t2, &wall_t2); //timer stop for hdf5 file write //compute import/export lists creation time time = wall_t2-wall_t1; MPI_Reduce(&time, &max_time, 1, MPI_DOUBLE, MPI_MAX, MPI_ROOT, OP_MPI_HDF5_WORLD); //print performance results if(my_rank == MPI_ROOT) { printf("Max hdf5 file write time = %lf\n\n",max_time); } MPI_Comm_free(&OP_MPI_HDF5_WORLD); }
int main (int argc, char *argv[]) { inp_t input_params; mdl_t source_mdl; net_t network; int cell_index; int verbose = 1; char *input_file; /* Parse options and command line arguments. Diplay help message if no (or more than one) argument is given. */ { int opt; static struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {0, 0, 0, 0} }; while ((opt = getopt_long (argc, argv, "hVvq", longopts, NULL)) != -1) { switch (opt) { case 'h': usage (); return EXIT_SUCCESS; break; case 'V': version (); return EXIT_SUCCESS; break; case 'v': verbose = 2; break; case 'q': verbose = 0; break; default: usage (); return EXIT_FAILURE; } }; argc -= optind; argv += optind; if (argc != 1) { usage (); return EXIT_FAILURE; } input_file = argv[0]; } /* Read the input file */ if( read_input_file_names (input_file, &input_params.files, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the chemical network file */ if( read_network (input_params.files.chem_file, &network, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the input file */ if( read_input (input_file, &input_params, &network, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the source model file */ if( read_source (input_params.files.source_file, &source_mdl, &input_params, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } // Hdf5 files, datatype and dataspace hid_t fid, datatype, dataspace, dataset, tsDataset, tsDataspace, speciesDataset, speciesDataspace, speciesType; datatype = H5Tcopy(H5T_NATIVE_DOUBLE); hsize_t dimsf[ ROUTE_DATASET_RANK ]={ source_mdl.n_cells, source_mdl.ts.n_time_steps, input_params.output.n_output_species, N_OUTPUT_ROUTES }; fid = H5Fcreate( "astrochem_output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); dataspace = H5Screate_simple( ABUNDANCE_DATASET_RANK, dimsf, NULL); // Add Atributes hid_t simpleDataspace = H5Screate(H5S_SCALAR); hid_t attrType = H5Tcopy(H5T_C_S1); H5Tset_size ( attrType, MAX_CHAR_FILENAME ); H5Tset_strpad(attrType,H5T_STR_NULLTERM); hid_t attrNetwork = H5Acreate( fid, "chem_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite( attrNetwork, attrType, input_params.files.chem_file); H5Aclose( attrNetwork ); hid_t attrModel = H5Acreate( fid, "source_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite( attrModel, attrType, input_params.files.source_file); H5Aclose( attrModel ); H5Tclose( attrType ); H5Sclose( simpleDataspace ); // Define chunk property hsize_t chunk_dims[ ROUTE_DATASET_RANK ] = { 1, 1, input_params.output.n_output_species, N_OUTPUT_ROUTES }; hid_t prop_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(prop_id, ABUNDANCE_DATASET_RANK , chunk_dims); // Create dataset dataset = H5Dcreate(fid, "Abundances", datatype, dataspace, H5P_DEFAULT, prop_id, H5P_DEFAULT); int i; hid_t dataspaceRoute, route_t_datatype, r_t_datatype, route_prop_id, routeGroup; hid_t routeDatasets[ input_params.output.n_output_species ]; if (input_params.output.trace_routes) { // Create route dataspace dataspaceRoute = H5Screate_simple( ROUTE_DATASET_RANK, dimsf, NULL); // Create route datatype r_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(r_t)); H5Tinsert( r_t_datatype, "reaction_number", HOFFSET(r_t, reaction_no ), H5T_NATIVE_INT); H5Tinsert( r_t_datatype, "reaction_rate", HOFFSET(r_t, rate), H5T_NATIVE_DOUBLE); route_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(rout_t)); H5Tinsert( route_t_datatype, "formation_rate", HOFFSET(rout_t, formation ), r_t_datatype ); H5Tinsert( route_t_datatype, "destruction_rate", HOFFSET(rout_t, destruction ), r_t_datatype ); // Define route chunk property route_prop_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk( route_prop_id, ROUTE_DATASET_RANK, chunk_dims); // Create each named route dataset routeGroup = H5Gcreate( fid, "Routes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); char routeName[7] = "route_"; char tempName[ MAX_CHAR_SPECIES + sizeof( routeName ) ]; for( i = 0; i < input_params.output.n_output_species ; i++ ) { strcpy( tempName, routeName ); strcat( tempName, network.species[input_params.output.output_species_idx[i]].name ); routeDatasets[i] = H5Dcreate( routeGroup, tempName, route_t_datatype, dataspaceRoute, H5P_DEFAULT, route_prop_id, H5P_DEFAULT); } } // Timesteps and species hsize_t n_ts = source_mdl.ts.n_time_steps; hsize_t n_species = input_params.output.n_output_species ; tsDataspace = H5Screate_simple( 1, &n_ts, NULL); speciesDataspace = H5Screate_simple( 1, &n_species, NULL); speciesType = H5Tcopy (H5T_C_S1); H5Tset_size (speciesType, MAX_CHAR_SPECIES ); H5Tset_strpad(speciesType,H5T_STR_NULLTERM); // Create ts and species datasets tsDataset = H5Dcreate(fid, "TimeSteps", datatype, tsDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); speciesDataset = H5Dcreate(fid, "Species", speciesType, speciesDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); double* convTs = (double*) malloc( sizeof(double)* source_mdl.ts.n_time_steps ); for( i=0; i< source_mdl.ts.n_time_steps; i++ ) { convTs[i] = source_mdl.ts.time_steps[i] / CONST_MKSA_YEAR; } H5Dwrite( tsDataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, convTs ); char speciesName [ input_params.output.n_output_species ][ MAX_CHAR_SPECIES ]; for( i = 0; i < input_params.output.n_output_species ; i++ ) { strcpy( speciesName[i], network.species[input_params.output.output_species_idx[i]].name ); } H5Dwrite( speciesDataset, speciesType, H5S_ALL, H5S_ALL, H5P_DEFAULT, speciesName ); free( convTs ); H5Dclose( tsDataset ); H5Dclose( speciesDataset ); H5Tclose( speciesType ); H5Sclose( tsDataspace ); H5Sclose( speciesDataspace ); #ifdef HAVE_OPENMP /*Initialize lock*/ omp_init_lock(&lock); /* Solve the ODE system for each cell. */ { #pragma omp parallel for schedule (dynamic, 1) #endif for (cell_index = 0; cell_index < source_mdl.n_cells; cell_index++) { if (verbose >= 1) fprintf (stdout, "Computing abundances in cell %d...\n", cell_index); if( full_solve ( fid, dataset, routeDatasets, dataspace, dataspaceRoute, datatype, route_t_datatype, cell_index, &input_params, source_mdl.mode, &source_mdl.cell[cell_index], &network, &source_mdl.ts, verbose) != EXIT_SUCCESS ) { exit (EXIT_FAILURE); } if (verbose >= 1) fprintf (stdout, "Done with cell %d.\n", cell_index); } #ifdef HAVE_OPENMP } /*Finished lock mechanism, destroy it*/ omp_destroy_lock(&lock); #endif /* * Close/release hdf5 resources. */ if (input_params.output.trace_routes) { for( i = 0; i < input_params.output.n_output_species ; i++ ) { H5Dclose(routeDatasets[i] ); } H5Sclose(dataspaceRoute); H5Gclose(routeGroup); H5Pclose(route_prop_id); H5Tclose(r_t_datatype); H5Tclose(route_t_datatype); } H5Dclose(dataset); H5Pclose(prop_id); H5Sclose(dataspace); H5Tclose(datatype); H5Fclose(fid); free_input (&input_params); free_mdl (&source_mdl); free_network (&network); return (EXIT_SUCCESS); }
op_map op_decl_map_hdf5(op_set from, op_set to, int dim, char const *file, char const *name) { //create new communicator int my_rank, comm_size; MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD); MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank); MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size); //MPI variables MPI_Info info = MPI_INFO_NULL; //HDF5 APIs definitions hid_t file_id; //file identifier hid_t plist_id; //property list identifier hid_t dset_id; //dataset identifier hid_t dataspace; //data space identifier hid_t memspace; //memory space identifier hsize_t count[2]; //hyperslab selection parameters hsize_t offset[2]; //Set up file access property list with parallel I/O access plist_id = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info); file_id = H5Fopen(file, H5F_ACC_RDONLY, plist_id ); H5Pclose(plist_id); /*find total size of this map by reading attributes*/ int g_size; //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute hid_t attr = H5Aopen(dset_id, "size", H5P_DEFAULT); //read attribute H5Aread(attr,H5T_NATIVE_INT,&g_size); H5Aclose(attr); H5Dclose(dset_id); //calculate local size of set for this mpi process int l_size = compute_local_size (g_size, comm_size, my_rank); //check if size is accurate if(from->size != l_size) { printf("map from set size %d in file %s and size %d do not match on rank %d\n", l_size,file,from->size, my_rank); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } /*find dim with available attributes*/ int map_dim = 0; //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute attr = H5Aopen(dset_id, "dim", H5P_DEFAULT); //read attribute H5Aread(attr,H5T_NATIVE_INT,&map_dim); H5Aclose(attr); H5Dclose(dset_id); if(map_dim != dim) { printf("map.dim %d in file %s and dim %d do not match\n",map_dim,file,dim); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } /*find type with available attributes*/ dataspace= H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 10); //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute attr = H5Aopen(dset_id, "type", H5P_DEFAULT); //read attribute char typ[10]; H5Aread(attr,atype,typ); H5Aclose(attr); H5Sclose(dataspace); H5Dclose(dset_id); /*read in map in hyperslabs*/ //Create the dataset with default properties and close dataspace. dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //Each process defines dataset in memory and reads from a hyperslab in the file. int disp = 0; int* sizes = (int *)xmalloc(sizeof(int)*comm_size); MPI_Allgather(&l_size, 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD); for(int i = 0; i<my_rank; i++)disp = disp + sizes[i]; free(sizes); count[0] = l_size; count[1] = dim; offset[0] = disp; offset[1] = 0; memspace = H5Screate_simple(2, count, NULL); //Select hyperslab in the file. dataspace = H5Dget_space(dset_id); H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); //Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE); //initialize data buffer and read data int* map = 0; if(strcmp(typ,"int") == 0) { map = (int *)xmalloc(sizeof(int)*l_size*dim); H5Dread(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, map); } else if (strcmp(typ,"long") == 0) { map = (int *)xmalloc(sizeof(long)*l_size*dim); H5Dread(dset_id, H5T_NATIVE_LONG, memspace, dataspace, plist_id, map); } else if (strcmp(typ,"long long") == 0) { map = (int *)xmalloc(sizeof(long)*l_size*dim); H5Dread(dset_id, H5T_NATIVE_LLONG, memspace, dataspace, plist_id, map); } else { printf("unknown type\n"); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } H5Pclose(plist_id); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dset_id); H5Fclose(file_id); MPI_Comm_free(&OP_MPI_HDF5_WORLD); return op_decl_map(from, to, dim, map, name); }
op_dat op_decl_dat_hdf5(op_set set, int dim, char const *type, char const *file, char const *name) { //create new communicator int my_rank, comm_size; MPI_Comm_dup(MPI_COMM_WORLD, &OP_MPI_HDF5_WORLD); MPI_Comm_rank(OP_MPI_HDF5_WORLD, &my_rank); MPI_Comm_size(OP_MPI_HDF5_WORLD, &comm_size); //MPI variables MPI_Info info = MPI_INFO_NULL; //HDF5 APIs definitions hid_t file_id; //file identifier hid_t plist_id; //property list identifier hid_t dset_id; //dataset identifier hid_t dataspace; //data space identifier hid_t memspace; //memory space identifier hsize_t count[2]; //hyperslab selection parameters hsize_t offset[2]; hid_t attr; //attribute identifier //Set up file access property list with parallel I/O access plist_id = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fapl_mpio(plist_id, OP_MPI_HDF5_WORLD, info); file_id = H5Fopen(file, H5F_ACC_RDONLY, plist_id ); H5Pclose(plist_id); /*find element size of this dat with available attributes*/ size_t dat_size = 0; //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute attr = H5Aopen(dset_id, "size", H5P_DEFAULT); //read attribute H5Aread(attr,H5T_NATIVE_INT,&dat_size); H5Aclose(attr); H5Dclose(dset_id); /*find dim with available attributes*/ int dat_dim = 0; //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute attr = H5Aopen(dset_id, "dim", H5P_DEFAULT); //read attribute H5Aread(attr,H5T_NATIVE_INT,&dat_dim); H5Aclose(attr); H5Dclose(dset_id); if(dat_dim != dim) { printf("dat.dim %d in file %s and dim %d do not match\n",dat_dim,file,dim); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } /*find type with available attributes*/ dataspace= H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 10); //open existing data set dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //get OID of the attribute attr = H5Aopen(dset_id, "type", H5P_DEFAULT); //read attribute char typ[10]; H5Aread(attr,atype,typ); H5Aclose(attr); H5Sclose(dataspace); H5Dclose(dset_id); if(strcmp(typ,type) != 0) { printf("dat.type %s in file %s and type %s do not match\n",typ,file,type); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } /*read in dat in hyperslabs*/ //Create the dataset with default properties and close dataspace. dset_id = H5Dopen(file_id, name, H5P_DEFAULT); //Each process defines dataset in memory and reads from a hyperslab in the file. int disp = 0; int* sizes = (int *)xmalloc(sizeof(int)*comm_size); MPI_Allgather(&(set->size), 1, MPI_INT, sizes, 1, MPI_INT, OP_MPI_HDF5_WORLD); for(int i = 0; i<my_rank; i++)disp = disp + sizes[i]; free(sizes); count[0] = set->size; count[1] = dim; offset[0] = disp; offset[1] = 0; memspace = H5Screate_simple(2, count, NULL); //Select hyperslab in the file. dataspace = H5Dget_space(dset_id); H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); //Create property list for collective dataset write. plist_id = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE); //initialize data buffer and read in data char* data = 0; if(strcmp(type,"double") == 0) { data = (char *)xmalloc(set->size*dim*sizeof(double)); H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, dataspace, plist_id, data); if(dat_size != dim*sizeof(double)) { printf("dat.size %lu in file %s and %d*sizeof(double) do not match\n",dat_size,file,dim); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } else dat_size = sizeof(double); }else if(strcmp(type,"float") == 0) { data = (char *)xmalloc(set->size*dim*sizeof(float)); H5Dread(dset_id, H5T_NATIVE_FLOAT, memspace, dataspace, plist_id, data); if(dat_size != dim*sizeof(float)) { printf("dat.size %lu in file %s and %d*sizeof(float) do not match\n",dat_size,file,dim); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } else dat_size = sizeof(float); }else if(strcmp(type,"int") == 0) { data = (char *)xmalloc(set->size*dim*sizeof(int)); H5Dread(dset_id, H5T_NATIVE_INT, memspace, dataspace, plist_id, data); if(dat_size != dim*sizeof(int)) { printf("dat.size %lu in file %s and %d*sizeof(int) do not match\n",dat_size,file,dim); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } else dat_size = sizeof(int); }else { printf("unknown type\n"); MPI_Abort(OP_MPI_HDF5_WORLD, 2); } H5Pclose(plist_id); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dset_id); H5Fclose(file_id); MPI_Comm_free(&OP_MPI_HDF5_WORLD); return op_decl_dat(set, dim, type, dat_size, data, name ); }
int main (void) { hid_t file, space, dset, attr; /* Handles */ herr_t status; hsize_t dims[2] = {DIM0, DIM1}; double wdata[DIM0][DIM1], /* Write buffer */ **rdata; /* Read buffer */ int ndims, i, j; /* * Initialize data. */ for (i=0; i<DIM0; i++) for (j=0; j<DIM1; j++) wdata[i][j] = (double) i / (j + 0.5) + j;; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset with a scalar dataspace. */ space = H5Screate (H5S_SCALAR); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); status = H5Sclose (space); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (2, dims, NULL); /* * Create the attribute and write the floating point data to it. * In this example we will save the data as 64 bit little endian * IEEE floating point numbers, regardless of the native type. The * HDF5 library automatically converts between different floating * point types. */ attr = H5Acreate (dset, ATTRIBUTE, H5T_IEEE_F64LE, space, H5P_DEFAULT); status = H5Awrite (attr, H5T_NATIVE_DOUBLE, wdata[0]); /* * Close and release resources. */ status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET); attr = H5Aopen_name (dset, ATTRIBUTE); /* * Get dataspace and allocate memory for read buffer. This is a * two dimensional attribute so the dynamic allocation must be done * in steps. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); /* * Allocate array of pointers to rows. */ rdata = (double **) malloc (dims[0] * sizeof (double *)); /* * Allocate space for floating point data. */ rdata[0] = (double *) malloc (dims[0] * dims[1] * sizeof (double)); /* * Set the rest of the pointers to rows to the correct addresses. */ for (i=1; i<dims[0]; i++) rdata[i] = rdata[0] + i * dims[1]; /* * Read the data. */ status = H5Aread (attr, H5T_NATIVE_DOUBLE, rdata[0]); /* * Output the data to the screen. */ printf ("%s:\n", ATTRIBUTE); for (i=0; i<dims[0]; i++) { printf (" ["); for (j=0; j<dims[1]; j++) printf (" %6.4f", rdata[i][j]); printf ("]\n"); } /* * Close and release resources. */ free (rdata[0]); free (rdata); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Fclose (file); return 0; }
int main (void) { hid_t file, space, dtype, dset, attr; /* Handles */ herr_t status; hsize_t dims[1] = {DIM0}; size_t len; char wdata[DIM0*LEN], /* Write buffer */ *rdata, /* Read buffer */ str[LEN] = "OPAQUE", *tag; int ndims, i, j; /* * Initialize data. */ for (i=0; i<DIM0; i++) { for (j=0; j<LEN-1; j++) wdata[j + i * LEN] = str[j]; wdata[LEN - 1 + i * LEN] = (char) i + '0'; } /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset with a null dataspace. */ space = H5Screate (H5S_NULL); dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Sclose (space); /* * Create opaque datatype and set the tag to something appropriate. * For this example we will write and view the data as a character * array. */ dtype = H5Tcreate (H5T_OPAQUE, LEN); status = H5Tset_tag (dtype, "Character array"); /* * Create dataspace. Setting maximum size to NULL sets the maximum * size to be the current size. */ space = H5Screate_simple (1, dims, NULL); /* * Create the attribute and write the opaque data to it. */ attr = H5Acreate (dset, ATTRIBUTE, dtype, space, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite (attr, dtype, wdata); /* * Close and release resources. */ status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (dtype); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the attribute has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). */ /* * Open file, dataset, and attribute. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET, H5P_DEFAULT); attr = H5Aopen (dset, ATTRIBUTE, H5P_DEFAULT); /* * Get datatype and properties for the datatype. Note that H5Tget_tag * allocates space for the string in tag, so we must remember to free() it * later. */ dtype = H5Aget_type (attr); len = H5Tget_size (dtype); tag = H5Tget_tag (dtype); /* * Get dataspace and allocate memory for read buffer. */ space = H5Aget_space (attr); ndims = H5Sget_simple_extent_dims (space, dims, NULL); rdata = (char *) malloc (dims[0] * len); /* * Read the data. */ status = H5Aread (attr, dtype, rdata); /* * Output the data to the screen. */ printf ("Datatype tag for %s is: \"%s\"\n", ATTRIBUTE, tag); for (i=0; i<dims[0]; i++) { printf ("%s[%u]: ",ATTRIBUTE,i); for (j=0; j<len; j++) printf ("%c", rdata[j + i * len]); printf ("\n"); } /* * Close and release resources. */ free (rdata); free (tag); status = H5Aclose (attr); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (dtype); status = H5Fclose (file); return 0; }
/*------------------------------------------------------------------------- * Function: test_multi * * Purpose: Tests the file handle interface for MUTLI driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * *------------------------------------------------------------------------- */ static herr_t test_multi(void) { hid_t file=(-1), fapl, fapl2=(-1), dset=(-1), space=(-1); hid_t root, attr, aspace, atype; hid_t access_fapl = -1; char filename[1024]; int *fhandle2=NULL, *fhandle=NULL; hsize_t file_size; H5FD_mem_t mt, memb_map[H5FD_MEM_NTYPES]; hid_t memb_fapl[H5FD_MEM_NTYPES]; haddr_t memb_addr[H5FD_MEM_NTYPES]; const char *memb_name[H5FD_MEM_NTYPES]; char sv[H5FD_MEM_NTYPES][32]; hsize_t dims[2]= {MULTI_SIZE, MULTI_SIZE}; hsize_t adims[1]= {1}; char dname[]="dataset"; char meta[] = "this is some metadata on this file"; int i, j; int buf[MULTI_SIZE][MULTI_SIZE]; TESTING("MULTI file driver"); /* Set file access property list for MULTI driver */ fapl = h5_fileaccess(); HDmemset(memb_map, 0, sizeof memb_map); HDmemset(memb_fapl, 0, sizeof memb_fapl); HDmemset(memb_name, 0, sizeof memb_name); HDmemset(memb_addr, 0, sizeof memb_addr); HDmemset(sv, 0, sizeof sv); for(mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; H5_INC_ENUM(H5FD_mem_t,mt)) { memb_fapl[mt] = H5P_DEFAULT; memb_map[mt] = H5FD_MEM_SUPER; } memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW; memb_map[H5FD_MEM_BTREE] = H5FD_MEM_BTREE; memb_map[H5FD_MEM_GHEAP] = H5FD_MEM_GHEAP; sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's'); memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER]; memb_addr[H5FD_MEM_SUPER] = 0; sprintf(sv[H5FD_MEM_BTREE], "%%s-%c.h5", 'b'); memb_name[H5FD_MEM_BTREE] = sv[H5FD_MEM_BTREE]; memb_addr[H5FD_MEM_BTREE] = HADDR_MAX/4; sprintf(sv[H5FD_MEM_DRAW], "%%s-%c.h5", 'r'); memb_name[H5FD_MEM_DRAW] = sv[H5FD_MEM_DRAW]; memb_addr[H5FD_MEM_DRAW] = HADDR_MAX/2; sprintf(sv[H5FD_MEM_GHEAP], "%%s-%c.h5", 'g'); memb_name[H5FD_MEM_GHEAP] = sv[H5FD_MEM_GHEAP]; memb_addr[H5FD_MEM_GHEAP] = (HADDR_MAX/4)*3; if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[4], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test wrong ways to reopen multi files */ if(test_multi_opens(filename) < 0) TEST_ERROR; /* Reopen the file */ if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create and write data set */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_MULTI != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Before any data is written, the raw data file is empty. So * the file size is only the size of b-tree + HADDR_MAX/4. */ if(file_size < HADDR_MAX/4 || file_size > HADDR_MAX/2) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i=0; i<MULTI_SIZE; i++) for(j=0; j<MULTI_SIZE; j++) buf[i][j] = i*10000+j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_SUPER) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_DRAW) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* After the data is written, the file size is huge because the * beginning of raw data file is set at HADDR_MAX/2. It's supposed * to be (HADDR_MAX/2 + 128*128*4) */ if(file_size < HADDR_MAX/2 || file_size > HADDR_MAX) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; /* Create and write attribute for the root group. */ if((root = H5Gopen2(file, "/", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Attribute string. */ if((atype = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR; if(H5Tset_size(atype, strlen(meta) + 1) < 0) TEST_ERROR; if(H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) TEST_ERROR; /* Create and write attribute */ if((aspace = H5Screate_simple(1, adims, NULL)) < 0) TEST_ERROR; if((attr = H5Acreate2(root, "Metadata", atype, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Awrite(attr, atype, meta) < 0) TEST_ERROR; /* Close IDs */ if(H5Tclose(atype) < 0) TEST_ERROR; if(H5Sclose(aspace) < 0) TEST_ERROR; if(H5Aclose(attr) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose(fapl); H5Pclose(fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
/*------------------------------------------------------------------------- * Function: add_records * * Purpose: Writes a specified number of records to random datasets in * the SWMR test file. * * Parameters: hid_t fid * The file ID of the SWMR HDF5 file * * unsigned verbose * Whether or not to emit verbose console messages * * unsigned long nrecords * # of records to write to the datasets * * unsigned long flush_count * # of records to write before flushing the file to disk * * Return: Success: 0 * Failure: -1 * *------------------------------------------------------------------------- */ static int add_records(hid_t fid, unsigned verbose, unsigned long nrecords, unsigned long flush_count) { hid_t tid; /* Datatype ID for records */ hid_t mem_sid; /* Memory dataspace ID */ hsize_t start[2] = {0, 0}; /* Hyperslab selection values */ hsize_t count[2] = {1, 1}; /* Hyperslab selection values */ symbol_t record; /* The record to add to the dataset */ H5AC_cache_config_t mdc_config_orig; /* Original metadata cache configuration */ H5AC_cache_config_t mdc_config_cork; /* Corked metadata cache configuration */ unsigned long rec_to_flush; /* # of records left to write before flush */ volatile int dummy; /* Dummy varialbe for busy sleep */ hsize_t dim[2] = {1,0}; /* Dataspace dimensions */ unsigned long u, v; /* Local index variables */ HDassert(fid >= 0); /* Reset the record */ /* (record's 'info' field might need to change for each record written, also) */ HDmemset(&record, 0, sizeof(record)); /* Create a dataspace for the record to add */ if((mem_sid = H5Screate(H5S_SCALAR)) < 0) return -1; /* Create datatype for appending records */ if((tid = create_symbol_datatype()) < 0) return -1; /* Get the current metadata cache configuration, and set up the corked * configuration */ mdc_config_orig.version = H5AC__CURR_CACHE_CONFIG_VERSION; if(H5Fget_mdc_config(fid, &mdc_config_orig) < 0) return -1; HDmemcpy(&mdc_config_cork, &mdc_config_orig, sizeof(mdc_config_cork)); mdc_config_cork.evictions_enabled = FALSE; mdc_config_cork.incr_mode = H5C_incr__off; mdc_config_cork.flash_incr_mode = H5C_flash_incr__off; mdc_config_cork.decr_mode = H5C_decr__off; /* Add records to random datasets, according to frequency distribution */ rec_to_flush = flush_count; for(u = 0; u < nrecords; u++) { symbol_info_t *symbol; /* Symbol to write record to */ hid_t file_sid; /* Dataset's space ID */ hid_t aid; /* Attribute ID */ /* Get a random dataset, according to the symbol distribution */ symbol = choose_dataset(); /* Cork the metadata cache, to prevent the object header from being * flushed before the data has been written */ /*if(H5Fset_mdc_config(fid, &mdc_config_cork) < 0) return(-1);*/ /* If this is the first time the dataset has been opened, extend it and * add the sequence attribute */ if(symbol->nrecords == 0) { symbol->nrecords = nrecords / 5; dim[1] = symbol->nrecords; if(H5Dset_extent(symbol->dsid, dim) < 0) return -1; if((file_sid = H5Screate(H5S_SCALAR)) < 0) return -1; if((aid = H5Acreate2(symbol->dsid, "seq", H5T_NATIVE_ULONG, file_sid, H5P_DEFAULT, H5P_DEFAULT)) < 0) return -1; if(H5Sclose(file_sid) < 0) return -1; } /* end if */ else if((aid = H5Aopen(symbol->dsid, "seq", H5P_DEFAULT)) < 0) return -1; /* Get the coordinate to write */ start[1] = (hsize_t)HDrandom() % symbol->nrecords; /* Set the record's ID (equal to its position) */ record.rec_id = start[1]; /* Get the dataset's dataspace */ if((file_sid = H5Dget_space(symbol->dsid)) < 0) return -1; /* Choose a random record in the dataset */ if(H5Sselect_hyperslab(file_sid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) return -1; /* Write record to the dataset */ if(H5Dwrite(symbol->dsid, tid, mem_sid, file_sid, H5P_DEFAULT, &record) < 0) return -1; /* Write the sequence number attribute. Since we synchronize the random * number seed, the readers will always generate the same sequence of * randomly chosen datasets and offsets. Therefore, and because of the * flush dependencies on the object header, the reader will be * guaranteed to see the written data if the sequence attribute is >=u. */ if(H5Awrite(aid, H5T_NATIVE_ULONG, &u) < 0) return -1; /* Close the attribute */ if(H5Aclose(aid) < 0) return -1; /* Uncork the metadata cache */ /*if(H5Fset_mdc_config(fid, &mdc_config_orig) < 0) return(-1);*/ /* Close the dataset's dataspace */ if(H5Sclose(file_sid) < 0) return -1; /* Check for flushing file */ if(flush_count > 0) { /* Decrement count of records to write before flushing */ rec_to_flush--; /* Check for counter being reached */ if(0 == rec_to_flush) { /* Flush contents of file */ if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0) return -1; /* Reset flush counter */ rec_to_flush = flush_count; } /* end if */ } /* end if */ #ifdef OUT /* Busy wait, to let readers catch up */ /* If this is removed, also remove the BUSY_WAIT symbol * at the top of the file. */ dummy = 0; for(v=0; v<BUSY_WAIT; v++) dummy++; if((unsigned long)dummy != v) return -1; #endif /* OUT */ } /* end for */ /* Close the memory dataspace */ if(H5Sclose(mem_sid) < 0) return -1; /* Close the datatype */ if(H5Tclose(tid) < 0) return -1; /* Emit informational message */ if(verbose) fprintf(stderr, "Closing datasets\n"); /* Close the datasets */ for(u = 0; u < NLEVELS; u++) for(v = 0; v < symbol_count[u]; v++) if(H5Dclose(symbol_info[u][v].dsid) < 0) return -1; return 0; }
int main (int argc, char ** argv) { char filename [256]; int rank, size, gidx, i, j, k,l; MPI_Comm comm_dummy = MPI_COMM_WORLD; /* MPI_Comm is defined through adios_read.h */ enum ADIOS_DATATYPES attr_type; void * data = NULL; uint64_t start[] = {0,0,0,0,0,0,0,0,0,0}; uint64_t count[MAX_DIMS], hcount[MAX_DIMS], bytes_read = 0; herr_t h5_err; char h5name[256],aname[256],fname[256]; int dims [MAX_DIMS]; int h5rank[MAX_DIMS]; int h5i, level; hid_t grp_id [GMAX+1], space_id, dataset_id; hid_t memspace_id, dataspace_id, att_id; char ** grp_name; hid_t type_id; hid_t h5_type_id; hsize_t adims; if (argc < 2) { printf("Usage: %s <BP-file> <HDF5-file>\n", argv[0]); return 1; } MPI_Init(&argc, &argv); h5_err = H5Eset_auto(NULL, NULL ); ADIOS_FILE * f = adios_fopen (argv[1], comm_dummy); HDF5_FILE = H5Fcreate(argv[2],H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* create the complex types for HDF5 */ complex_real_id = H5Tcreate (H5T_COMPOUND, sizeof (complex_real_t)); H5Tinsert (complex_real_id, "real", HOFFSET(complex_real_t,re), H5T_NATIVE_FLOAT); H5Tinsert (complex_real_id, "imaginary", HOFFSET(complex_real_t,im), H5T_NATIVE_FLOAT); complex_double_id = H5Tcreate (H5T_COMPOUND, sizeof (complex_double_t)); H5Tinsert (complex_double_id, "real", HOFFSET(complex_double_t,re), H5T_NATIVE_DOUBLE); H5Tinsert (complex_double_id, "imaginary", HOFFSET(complex_double_t,im), H5T_NATIVE_DOUBLE); if (f == NULL) { if (DEBUG) printf ("%s\n", adios_errmsg()); return -1; } /* For all groups */ for (gidx = 0; gidx < f->groups_count; gidx++) { if (DEBUG) printf("Group %s:\n", f->group_namelist[gidx]); ADIOS_GROUP * g = adios_gopen (f, f->group_namelist[gidx]); if (g == NULL) { if (DEBUG) printf ("%s\n", adios_errmsg()); return -1; } /* First create all of the groups */ grp_id [0] = HDF5_FILE; for (i = 0; i < g->vars_count; i++) { ADIOS_VARINFO * v = adios_inq_var_byid (g, i); strcpy(h5name,g->var_namelist[i]); grp_name = bp_dirparser (h5name, &level); for (j = 0; j < level-1; j++) { grp_id [j + 1] = H5Gopen (grp_id [j], grp_name [j]); if (grp_id [j + 1] < 0) { grp_id [j + 1] = H5Gcreate (grp_id [j], grp_name [j], 0); } } for (j=1; j<level; j++) { H5Gclose(grp_id[j]); } } /* Now we can write data into these scalars */ /* For all variables */ if (DEBUG) printf(" Variables=%d:\n", g->vars_count); for (i = 0; i < g->vars_count; i++) { ADIOS_VARINFO * v = adios_inq_var_byid (g, i); uint64_t total_size = adios_type_size (v->type, v->value); for (j = 0; j < v->ndim; j++) total_size *= v->dims[j]; strcpy(h5name,g->var_namelist[i]); if (DEBUG) printf(" %-9s %s", adios_type_to_string(v->type), g->var_namelist[i]); h5_err = bp_getH5TypeId (v->type, &h5_type_id); if (v->type==adios_string) H5Tset_size(h5_type_id,strlen(v->value)); if (v->ndim == 0) { /* Scalars do not need to be read in, we get it from the metadata when using adios_inq_var */ if (DEBUG) printf(" = %s\n", value_to_string(v->type, v->value, 0)); // add the hdf5 dataset, these are scalars for (h5i = 0;h5i<MAX_DIMS;h5i++) count[0] = 0; count[0] = 1; // we are writing just 1 element, RANK=1 h5_err = bp_getH5TypeId (v->type, &h5_type_id); H5LTmake_dataset(HDF5_FILE,h5name,1,count,h5_type_id,v->value); } else { h5_err = readVar(g, v, h5name); } adios_free_varinfo (v); } /* variables */ /* For all attributes */ if (DEBUG) printf(" Attributes=%d:\n", g->attrs_count); for (i = 0; i < g->attrs_count; i++) { enum ADIOS_DATATYPES atype; int asize; void *adata; adios_get_attr_byid (g, i, &atype, &asize, &adata); grp_name = bp_dirparser (g->attr_namelist[i], &level); strcpy(aname,grp_name[level-1]); // the name of the attribute is the last in the array // we then need to concat the rest together strcpy(fname,"/"); for (j=0;j<level-1;j++) { strcat(fname,grp_name[j]); } h5_err = bp_getH5TypeId (atype, &h5_type_id); // let's create the attribute adims = 1; if (atype==adios_string) H5Tset_size(h5_type_id,strlen(adata)); space_id = H5Screate(H5S_SCALAR); // just a scalar att_id = H5Acreate(HDF5_FILE, g->attr_namelist[i], h5_type_id, space_id,H5P_DEFAULT); h5_err = H5Awrite(att_id, h5_type_id, adata); h5_err = H5Aclose(att_id); h5_err = H5Sclose(space_id); if (DEBUG) printf(" %-9s %s = %s\n", adios_type_to_string(atype), g->attr_namelist[i], value_to_string(atype, adata, 0)); free(adata); } /* attributes */ adios_gclose (g); } /* groups */ adios_fclose (f); h5_err = H5Fclose(HDF5_FILE); MPI_Finalize(); return 0; }
/*------------------------------------------------------------------------- * Function: gent_att_compound_vlstr * * Purpose: Generate a dataset and a group. * Both has an attribute with a compound datatype consisting * of a variable length string * *------------------------------------------------------------------------- */ static void gent_att_compound_vlstr(hid_t loc_id) { typedef struct { /* Compound structure for the attribute */ int i; char *v; } s1; hsize_t dim[1] = {1}; /* Dimension size */ hid_t sid = -1; /* Dataspace ID */ hid_t tid = -1; /* Datatype ID */ hid_t aid = -1; /* Attribute ID */ hid_t did = -1; /* Dataset ID */ hid_t gid = -1; /* Group ID */ hid_t vl_str_tid = -1; /* Variable length datatype ID */ hid_t cmpd_tid = -1; /* Compound datatype ID */ hid_t null_sid = -1; /* Null dataspace ID */ s1 buf; /* Buffer */ buf.i = 9; buf.v = "ThisIsAString"; /* Create an integer datatype */ if((tid = H5Tcopy(H5T_NATIVE_INT)) < 0) goto error; /* Create a variable length string */ if((vl_str_tid = H5Tcopy(H5T_C_S1)) < 0) goto error; if(H5Tset_size(vl_str_tid, H5T_VARIABLE) < 0) goto error; /* Create a compound datatype with a variable length string and an integer */ if((cmpd_tid = H5Tcreate(H5T_COMPOUND, sizeof(s1))) < 0) goto error; if(H5Tinsert(cmpd_tid, "i", HOFFSET(s1, i), tid) < 0) goto error; if(H5Tinsert(cmpd_tid, "v", HOFFSET(s1, v), vl_str_tid) < 0) goto error; /* Create a dataset */ if((null_sid = H5Screate(H5S_NULL)) < 0) goto error; if((did = H5Dcreate2(loc_id, DATASET_ATTR, H5T_NATIVE_INT, null_sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Attach an attribute with the compound datatype to the dataset */ if((sid = H5Screate_simple(1, dim, dim)) < 0) goto error; if((aid = H5Acreate2(did, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Write the attribute */ buf.i = 9; buf.v = "ThisIsAString"; if(H5Awrite(aid, cmpd_tid, &buf) < 0) goto error; /* Close the dataset and its attribute */ if(H5Dclose(did) < 0) goto error; if(H5Aclose(aid) < 0) goto error; /* Create a group */ if((gid = H5Gcreate2(loc_id, GROUP_ATTR, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Attach an attribute with the compound datatype to the group */ if((aid = H5Acreate2(gid, ATTR, cmpd_tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Awrite(aid, cmpd_tid, &buf) < 0) goto error; /* Close the group and its attribute */ if(H5Aclose(aid) < 0) goto error; if(H5Gclose(gid) < 0) goto error; /* Close dataspaces */ if(H5Sclose(sid) < 0) goto error; if(H5Sclose(null_sid) < 0) goto error; /* Close datatypes */ if(H5Tclose(tid) < 0) goto error; if(H5Tclose(vl_str_tid) < 0) goto error; if(H5Tclose(cmpd_tid) < 0) goto error; error: H5E_BEGIN_TRY { H5Tclose(tid); H5Tclose(vl_str_tid); H5Tclose(cmpd_tid); H5Sclose(null_sid); H5Sclose(sid); H5Dclose(did); H5Aclose(aid); H5Gclose(gid); } H5E_END_TRY; } /* gen_att_compound_vlstr() */
int main( int argc, char ** argv ) { hid_t file_id,group_id,header_id; hid_t hdf5_dataspace,hdf5_attribute; hsize_t pdims[2]; hsize_t mdims[1]; hsize_t tdims[1]; hsize_t fdims[1]; float positions[NPARTS*NDIMS]; float velocities[NPARTS*NDIMS]; int IDs[NPARTS]; int nFiles=1; int Npart[NTYPES]={0,0,0,0,NPARTS,0}; int Npart_hw[NTYPES]={0,0,0,0,0,0}; #ifdef HAS_CORE double core_fraction=0.1; double disk_mass=(1.0-core_fraction)*(500*400/1.0e10); double core_mass=disk_mass*core_fraction/(1.0-core_fraction); double mass_per=disk_mass/(NPARTS-1); #else double disk_mass=(500*400/1.0e10); double mass_per=disk_mass/(NPARTS); #endif //double Massarr[NTYPES]={0.0,0.0,0.0,0.0,mass_per,0.0}; float masses[NPARTS]; herr_t status; double redshift=0.0; double time=0.0; double boxsize=0.0; double dzero=0.0; double done=1.0; int izero=0; int ione=1; double distance; double radius=0.77; double base_vel=0.0; double r,rscaled,a,v,cosine,sine,con,posx,posy; double rotate_factor=1.0; double G=6.67e-8; // cm^3 g^-1 s^-2 char filename[80]; int i,j,k; seed_by_time(0); if (argc>1) { sscanf(argv[1],"%s",filename); } else { strcpy(filename,"gal_test.hdf5"); } #ifdef HAS_CORE masses[0]=core_mass; for(i=1;i<NPARTS;i++) { masses[i]=mass_per; } #else for(i=0;i<NPARTS;i++) { masses[i]=mass_per; } #endif positions[0]=0.0; positions[1]=0.0; positions[2]=0.0; for(i=1;i<NPARTS;i++) { distance=2.0*radius; while(distance>radius) { distance=0.0; for(j=0;j<NDIMS;j++) { positions[3*i+j] = radius*(1.0-2.0*(double)rand()/(double)RAND_MAX); distance+=pow(positions[3*i+j],2.0); } distance=sqrt(distance); } } /* random velocity for(i=0;i<NPARTS;i++) { for(j=0;j<NDIMS;j++) { velocities[3*i+j] = base_vel*(1.0-2.0*(double)rand()/(double)RAND_MAX); } } */ velocities[0]=0.0; velocities[1]=0.0; velocities[2]=0.0; for(i=1;i<NPARTS;i++) { posx = positions[NDIMS*i]; posy = positions[NDIMS*i+1]; r=sqrt(pow(posx,2.0)+pow(posy,2.0)); #ifdef HAS_CORE con = G*(disk_mass+core_mass)*1.99e43*pow((r/radius),3.0); #else con = G*(disk_mass)*1.99e43*pow((r/radius),3.0); #endif rscaled=r*3.089e21; // convert to cm if(r>0.0) { v=sqrt(con/rscaled)*1.0e-5; cosine=posx/r; sine=posy/r; velocities[NDIMS*i+0]=v*rotate_factor*(-sine); velocities[NDIMS*i+1]=v*rotate_factor*cosine; velocities[NDIMS*i+2]=0.0; } } for(i=0;i<NPARTS;i++) { IDs[i]=i; } // EXAMPLE("make a dataset"); file_id = H5Fcreate (filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); group_id = H5Gcreate(file_id, "PartType4", 0); header_id = H5Gcreate(file_id, "Header", 0); pdims[0] = NPARTS; pdims[1] = NDIMS; mdims[0] = NPARTS; tdims[0] = NTYPES; fdims[0] = 1; hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate(header_id, "NumPart_ThisFile", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, Npart); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate(header_id, "NumPart_Total", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate(header_id, "NumPart_Total_HW", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); /* hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate(header_id, "MassTable", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, Massarr); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); */ hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Time", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &time); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Redshift", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &redshift); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "BoxSize", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &boxsize); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "NumFilesPerSnapshot", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &nFiles); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Omega0", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &dzero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "OmegaLambda", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &dzero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "HubbleParam", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &done); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Flag_Sfr", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &izero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Flag_Cooling", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &izero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Flag_StellarAge", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &izero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Flag_Metals", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &izero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate(header_id, "Flag_Feedback", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &izero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate(header_id, "Flag_Entropy_ICs", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); status = H5LTmake_dataset(file_id,"PartType4/Coordinates",2, pdims,H5T_NATIVE_FLOAT,positions); status = H5LTmake_dataset(file_id,"PartType4/ParticleIDs",1, mdims,H5T_NATIVE_UINT,IDs); status = H5LTmake_dataset(file_id,"PartType4/Velocities",2, pdims,H5T_NATIVE_FLOAT,velocities); status = H5LTmake_dataset(file_id,"PartType4/Masses",1, mdims,H5T_NATIVE_FLOAT,masses); status = H5Fclose (file_id); // PASSED(); return 0; }
/* ** WARNING : this function returns an allocated value that must be freed. */ static char* readAttribute(int _iDatasetId, const char *_pstName) { hid_t iAttributeId; hid_t iFileType, memtype, iSpace; herr_t status; hsize_t dims[1]; size_t iDim; hsize_t n = 0; char *pstValue = NULL; if (H5Aiterate(_iDatasetId, H5_INDEX_NAME, H5_ITER_NATIVE, &n, find_attr_by_name, (void *)_pstName) > 0) { iAttributeId = H5Aopen(_iDatasetId, _pstName, H5P_DEFAULT); if (iAttributeId < 0) { return NULL; } /* * Get the datatype and its size. */ iFileType = H5Aget_type(iAttributeId); iDim = H5Tget_size(iFileType); iDim++; /* Make room for null terminator */ /* * Get dataspace and allocate memory for read buffer. This is a * two dimensional attribute so the dynamic allocation must be done * in steps. */ iSpace = H5Aget_space(iAttributeId); if (iSpace < 0) { return NULL; } status = H5Sget_simple_extent_dims(iSpace, dims, NULL); if (status < 0) { return NULL; } /* * Allocate space for string data. */ pstValue = (char *)MALLOC((size_t) ((dims[0] * iDim + 1) * sizeof(char))); /* * Create the memory datatype. */ memtype = H5Tcopy(H5T_C_S1); status = H5Tset_size(memtype, iDim); if (status < 0) { FREE(pstValue); return NULL; } /* * Read the data. */ status = H5Aread(iAttributeId, memtype, pstValue); if (status < 0) { FREE(pstValue); return NULL; } status = H5Tclose(memtype); if (status < 0) { FREE(pstValue); return NULL; } status = H5Sclose(iSpace); if (status < 0) { FREE(pstValue); return NULL; } status = H5Tclose(iFileType); if (status < 0) { FREE(pstValue); return NULL; } status = H5Aclose(iAttributeId); if (status < 0) { FREE(pstValue); return NULL; } } return pstValue; }
static void gent_ub(const char * filename, size_t ub_size, size_t ub_fill) { hid_t fid, group, attr, dataset, space; hid_t create_plist; hsize_t dims[2]; int data[2][2], dset1[10][10], dset2[20]; char buf[BUF_SIZE]; int i, j; size_t u; float dset2_1[10], dset2_2[3][5]; int fd; char *bp; if(ub_size > 0) { create_plist = H5Pcreate(H5P_FILE_CREATE); H5Pset_userblock(create_plist, (hsize_t)ub_size); fid = H5Fcreate(filename, H5F_ACC_TRUNC, create_plist, H5P_DEFAULT); } else { fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); } /* create groups */ group = H5Gcreate2(fid, "/g1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gcreate2(fid, "/g1/g1.2/g1.2.1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); /* root attributes */ group = H5Gopen2(fid, "/", H5P_DEFAULT); dims[0] = 10; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(group, "attr1", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "abcdefghi"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); dims[0] = 2; dims[1] = 2; space = H5Screate_simple(2, dims, NULL); attr = H5Acreate2(group, "attr2", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT); data[0][0] = 0; data[0][1] = 1; data[1][0] = 2; data[1][1] = 3; H5Awrite(attr, H5T_NATIVE_INT, data); H5Sclose(space); H5Aclose(attr); H5Gclose(group); group = H5Gopen2(fid, "/g1/g1.1", H5P_DEFAULT); /* dset1.1.1 */ dims[0] = 10; dims[1] = 10; space = H5Screate_simple(2, dims, NULL); dataset = H5Dcreate2(group, "dset1.1.1", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) dset1[i][j] = j*i; H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset1); H5Sclose(space); /* attributes of dset1.1.1 */ dims[0] = 27; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(dataset, "attr1", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "1st attribute of dset1.1.1"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); dims[0] = 27; space = H5Screate_simple(1, dims, NULL); attr = H5Acreate2(dataset, "attr2", H5T_STD_I8BE, space, H5P_DEFAULT, H5P_DEFAULT); sprintf(buf, "2nd attribute of dset1.1.1"); H5Awrite(attr, H5T_NATIVE_SCHAR, buf); H5Sclose(space); H5Aclose(attr); H5Dclose(dataset); /* dset1.1.2 */ dims[0] = 20; space = H5Screate_simple(1, dims, NULL); dataset = H5Dcreate2(group, "dset1.1.2", H5T_STD_I32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 20; i++) dset2[i] = i; H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2); H5Sclose(space); H5Dclose(dataset); H5Gclose(group); /* external link */ H5Lcreate_external("somefile", "somepath", fid, "/g1/g1.2/extlink", H5P_DEFAULT, H5P_DEFAULT); /* soft link */ group = H5Gopen2(fid, "/g1/g1.2/g1.2.1", H5P_DEFAULT); H5Lcreate_soft("somevalue", group, "slink", H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group); group = H5Gopen2(fid, "/g2", H5P_DEFAULT); /* dset2.1 */ dims[0] = 10; space = H5Screate_simple(1, dims, NULL); dataset = H5Dcreate2(group, "dset2.1", H5T_IEEE_F32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 10; i++) dset2_1[i] = (float)(i*0.1F+1); H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2_1); H5Sclose(space); H5Dclose(dataset); /* dset2.2 */ dims[0] = 3; dims[1] = 5; space = H5Screate_simple(2, dims, NULL); dataset = H5Dcreate2(group, "dset2.2", H5T_IEEE_F32BE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) dset2_2[i][j] = (float)((i+1)*j*0.1F); H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset2_2); H5Sclose(space); H5Dclose(dataset); H5Gclose(group); /* user-defined link */ H5Lregister(UD_link_class); H5Lcreate_ud(fid, "/g2/udlink", (H5L_type_t)MY_LINKCLASS, NULL, (size_t)0, H5P_DEFAULT, H5P_DEFAULT); H5Fclose(fid); /* If a user block is being used, write to it here */ if(ub_size > 0) { HDassert(ub_size <= BUF_SIZE); fd = HDopen(filename, O_RDWR, 0); HDassert(fd >= 0); /* fill buf with pattern */ HDmemset(buf, '\0', ub_size); bp = buf; for (u = 0; u < ub_fill; u++) *bp++ = pattern[u % 10]; HDwrite(fd, buf, ub_size); HDclose(fd); } }
/* * Generate an HDF5 file with groups, datasets, attributes for testing the options: * -l N (--links=N): Set the threshold for # of links when printing information for small groups. * -m N (--dims=N): Set the threshold for the # of dimension sizes when printing information for small datasets. * -a N (--numattrs=N): Set the threshold for the # of attributes when printing information for small # of attributes. */ static void gen_threshold_file(const char *fname) { hid_t fid; /* File ID */ hid_t sid0, sid1, sid2, sid3, sid4; /* Dataspace IDs */ hid_t did; /* Dataset ID */ hid_t attr_id; /* Attribute ID */ hid_t gid; /* Group ID */ hsize_t two_dims[] = {2, 5}; /* Dimension array */ hsize_t one_dims[] = {6}; /* Dimension array */ hsize_t zero_dims[] = {0}; /* Dimension array */ char name[30]; /* Name */ unsigned i; /* Local index variable */ /* Create file */ if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create 1-D dataspace with zero dimension size */ if((sid0 = H5Screate_simple(1, zero_dims, NULL)) < 0) goto error; /* Create 1-D dataspace with non-zero dimension size*/ if((sid1 = H5Screate_simple(1, one_dims, NULL)) < 0) goto error; /* Create 2-D dataspace */ if((sid2 = H5Screate_simple(2, two_dims, NULL)) < 0) goto error; /* Create scalar dataspace */ if((sid3 = H5Screate(H5S_SCALAR)) < 0) goto error; /* Create null dataspace */ if((sid4 = H5Screate(H5S_NULL)) < 0) goto error; /* Create an attribute for the root group */ if((attr_id = H5Acreate2(fid, "attr", H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Aclose(attr_id) < 0) goto error; /* Create 1-D dataset with zero dimension size for the root group */ if((did = H5Dcreate2(fid, "zero_dset", H5T_NATIVE_UCHAR, sid0, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create 11 attributes for the dataset */ for(i = 1; i <= (THRES_NUM+1); i++) { sprintf(name, "%s%d", THRES_ATTR_NAME,i); if((attr_id = H5Acreate2(did, name, H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Aclose(attr_id) < 0) goto error; } if(H5Dclose(did) < 0) goto error; /* Create dataset with scalar dataspace for the root group */ if((did = H5Dcreate2(fid, "scalar_dset", H5T_NATIVE_UCHAR, sid3, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Dclose(did) < 0) goto error; /* Create dataset with null dataspace for the root group */ if((did = H5Dcreate2(fid, "null_dset", H5T_NATIVE_UCHAR, sid4, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Dclose(did) < 0) goto error; /* Create 2-D dataset for the root group */ if((did = H5Dcreate2(fid, "dset", H5T_NATIVE_UCHAR, sid2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create 10 attributes for the 2-D dataset */ for(i = 1; i <= THRES_NUM; i++) { sprintf(name, "%s%d", THRES_ATTR_NAME,i); if((attr_id = H5Acreate2(did, name, H5T_NATIVE_INT, sid1, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Aclose(attr_id) < 0) goto error; } if(H5Dclose(did) < 0) goto error; /* Create first group */ if((gid = H5Gcreate2(fid, "group1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create an attribute for the group */ if((attr_id = H5Acreate2(gid, "ATTR", H5T_NATIVE_INT, sid3, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Close attribute */ if(H5Aclose(attr_id) < 0) goto error; /* Create 10 1-D datasets with non-zero dimension size for the group */ for(i = 1; i <= THRES_NUM; i++) { /* set up dataset name */ sprintf(name, "%s%d", THRES_DSET_NAME,i); /* Create the dataset */ if((did = H5Dcreate2(gid, name, H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Close the dataset */ if(H5Dclose(did) < 0) goto error; } /* Close the group */ if(H5Gclose(gid) < 0) goto error; /* Create second group */ if((gid = H5Gcreate2(fid, "group2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create 25 attributes for the group */ for(i = 1; i <= THRES_NUM_25; i++) { /* Set up attribute name */ sprintf(name, "%s%d", THRES_ATTR_GRP_NAME,i); /* Create the attribute */ if((attr_id = H5Acreate2(gid, name, H5T_NATIVE_INT, sid2, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Close the attribute */ if(H5Aclose(attr_id) < 0) goto error; } /* Close the group */ if(H5Gclose(gid) < 0) goto error; /* Create third group */ if((gid = H5Gcreate2(fid, "group3", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create 9 1-D datasets with non-zero dimension size for the group */ for(i = 1; i < THRES_NUM; i++) { /* set up dataset name */ sprintf(name, "%s%d", THRES_DSET_NAME,i); /* Create the dataset */ if((did = H5Dcreate2(gid, name, H5T_NATIVE_UCHAR, sid1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Close the dataset */ if(H5Dclose(did) < 0) goto error; } /* Close the group */ if(H5Gclose(gid) < 0) goto error; /* Close dataspaces */ if(H5Sclose(sid0) < 0) goto error; if(H5Sclose(sid1) < 0) goto error; if(H5Sclose(sid2) < 0) goto error; if(H5Sclose(sid3) < 0) goto error; if(H5Sclose(sid4) < 0) goto error; /* Close file */ if(H5Fclose(fid) < 0) goto error; error: H5E_BEGIN_TRY { H5Gclose(gid); H5Aclose(attr_id); H5Dclose(did); H5Sclose(sid0); H5Sclose(sid1); H5Sclose(sid2); H5Sclose(sid3); H5Sclose(sid4); H5Fclose(fid); } H5E_END_TRY; } /* gen_threshold_file() */
/*------------------------------------------------------------------------- * Function: open_skeleton * * Purpose: Opens the SWMR HDF5 file and datasets. * * Parameters: const char *filename * The filename of the SWMR HDF5 file to open * * unsigned verbose * Whether or not to emit verbose console messages * * Return: Success: The file ID of the opened SWMR file * The dataset IDs are stored in a global array * * Failure: -1 * *------------------------------------------------------------------------- */ static hid_t open_skeleton(const char *filename, unsigned verbose) { hid_t fid; /* File ID for new HDF5 file */ hid_t fapl; /* File access property list */ hid_t aid; /* Attribute ID */ unsigned seed; /* Seed for random number generator */ unsigned u, v; /* Local index variable */ HDassert(filename); /* Create file access property list */ if((fapl = h5_fileaccess()) < 0) return -1; /* Set to use the latest library format */ if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) return -1; #ifdef QAK /* Increase the initial size of the metadata cache */ { H5AC_cache_config_t mdc_config; mdc_config.version = H5AC__CURR_CACHE_CONFIG_VERSION; H5Pget_mdc_config(fapl, &mdc_config); HDfprintf(stderr, "mdc_config.initial_size = %lu\n", (unsigned long)mdc_config.initial_size); HDfprintf(stderr,"mdc_config.epoch_length = %lu\n", (unsigned long)mdc_config.epoch_length); mdc_config.set_initial_size = 1; mdc_config.initial_size = 16 * 1024 * 1024; /* mdc_config.epoch_length = 5000; */ H5Pset_mdc_config(fapl, &mdc_config); } #endif /* QAK */ #ifdef QAK H5Pset_fapl_log(fapl, "append.log", H5FD_LOG_ALL, (size_t)(512 * 1024 * 1024)); #endif /* QAK */ /* Open the file */ if((fid = H5Fopen(filename, H5F_ACC_RDWR | H5F_ACC_SWMR_WRITE, fapl)) < 0) return -1; /* Close file access property list */ if(H5Pclose(fapl) < 0) return -1; /* Emit informational message */ if(verbose) fprintf(stderr, "Opening datasets\n"); /* Seed the random number generator with the attribute in the file */ if((aid = H5Aopen(fid, "seed", H5P_DEFAULT)) < 0) return -1; if(H5Aread(aid, H5T_NATIVE_UINT, &seed) < 0) return -1; if(H5Aclose(aid) < 0) return -1; HDsrandom(seed); /* Open the datasets */ for(u = 0; u < NLEVELS; u++) for(v = 0; v < symbol_count[u]; v++) { if((symbol_info[u][v].dsid = H5Dopen2(fid, symbol_info[u][v].name, H5P_DEFAULT)) < 0) return(-1); symbol_info[u][v].nrecords = 0; } /* end for */ return fid; }
/* * Generate HDF5 file with latest format with * NUM_GRPS groups and NUM_ATTRS attributes for the dataset * */ static void gen_newgrat_file(const char *fname) { hid_t fcpl; /* File creation property */ hid_t fapl; /* File access property */ hid_t fid; /* File id */ hid_t gid; /* Group id */ hid_t tid; /* Datatype id */ hid_t sid; /* Dataspace id */ hid_t attr_id; /* Attribute id */ hid_t did; /* Dataset id */ char name[30]; /* Group name */ char attrname[30]; /* Attribute name */ int i; /* Local index variable */ /* Get a copy file access property list */ if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) goto error; /* Set to use latest library format */ if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) goto error; /* Get a copy of file creation property list */ if((fcpl = H5Pcreate(H5P_FILE_CREATE)) < 0) goto error; /* Set file space handling strategy */ if(H5Pset_file_space(fcpl, H5F_FILE_SPACE_ALL_PERSIST, (hsize_t)0) < 0) goto error; /* Create file */ if((fid = H5Fcreate(fname, H5F_ACC_TRUNC, fcpl, fapl)) < 0) goto error; /* Create NUM_GRPS groups in the root group */ for(i = 1; i <= NUM_GRPS; i++) { sprintf(name, "%s%d", GROUP_NAME,i); if((gid = H5Gcreate2(fid, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Gclose(gid) < 0) goto error; } /* end for */ /* Create a datatype to commit and use */ if((tid = H5Tcopy(H5T_NATIVE_INT)) < 0) goto error; /* Create dataspace for dataset */ if((sid = H5Screate(H5S_SCALAR)) < 0) goto error; /* Create dataset */ if((did = H5Dcreate2(fid, DATASET_NAME, tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create NUM_ATTRS for the dataset */ for(i = 1; i <= NUM_ATTRS; i++) { sprintf(attrname, "%s%d", ATTR_NAME,i); if((attr_id = H5Acreate2(did, attrname, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Aclose(attr_id) < 0) goto error; } /* end for */ /* Close dataset, dataspace, datatype, file */ if(H5Dclose(did) < 0) goto error; if(H5Sclose(sid) < 0) goto error; if(H5Tclose(tid) < 0) goto error; if(H5Fclose(fid) < 0) goto error; error: H5E_BEGIN_TRY { H5Aclose(attr_id); H5Dclose(did); H5Tclose(tid); H5Sclose(sid); H5Gclose(gid); H5Fclose(fid); } H5E_END_TRY; } /* gen_newgrat_file() */
herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *pszAttrName, void *pDS ) { hid_t hAttrID; hid_t hAttrTypeID; hid_t hAttrNativeType; hid_t hAttrSpace; char *szData = NULL; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t nAttrSize; hsize_t i; void *buf = NULL; unsigned int nAttrDims; char **papszTokens; HDF5Dataset *poDS; CPLString osKey; char *szValue = NULL; poDS = (HDF5Dataset *) pDS; // Convert "/" into "_" for the path component const char* pszPath = poDS->poH5CurrentObject->pszUnderscorePath; if(pszPath != NULL && strlen(pszPath) > 0) { papszTokens = CSLTokenizeString2( pszPath, "/", CSLT_HONOURSTRINGS ); for( int i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if( i != 0) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); } // Convert whitespaces into "_" for the attribute name component papszTokens = CSLTokenizeString2( pszAttrName, " ", CSLT_STRIPLEADSPACES | CSLT_STRIPENDSPACES ); for( int i = 0; papszTokens != NULL && papszTokens[i] != NULL; ++i ) { if(!osKey.empty()) osKey += '_'; osKey += papszTokens[i]; } CSLDestroy( papszTokens ); hAttrID = H5Aopen_name( hH5ObjID, pszAttrName ); hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElmts *= (int) nSize[i]; } if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { if ( H5Tis_variable_str(hAttrNativeType) ) { char** papszStrings; papszStrings = (char**) CPLMalloc( nAttrElmts * sizeof(char*) ); // Read the values H5Aread( hAttrID, hAttrNativeType, papszStrings ); // Concatenate all values as one string (separated by a space) CPLString osVal = papszStrings[0]; for( i=1; i < nAttrElmts; i++ ) { osVal += " "; osVal += papszStrings[i]; } szValue = (char*) CPLMalloc(osVal.length() + 1); strcpy( szValue, osVal.c_str() ); H5Dvlen_reclaim( hAttrNativeType, hAttrSpace, H5P_DEFAULT, papszStrings ); CPLFree( papszStrings ); } else { nAttrSize = H5Aget_storage_size( hAttrID ); szValue = (char*) CPLMalloc((size_t) (nAttrSize+1)); H5Aread( hAttrID, hAttrNativeType, szValue ); szValue[nAttrSize] = '\0'; } } else { if( nAttrElmts > 0 ) { buf = (void *) CPLMalloc( nAttrElmts* H5Tget_size( hAttrNativeType )); szData = (char*) CPLMalloc( 8192 ); szValue = (char*) CPLMalloc( MAX_METADATA_LEN ); szData[0] = '\0'; szValue[0] ='\0'; H5Aread( hAttrID, hAttrNativeType, buf ); } if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) ){ for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c ", ((char *) buf)[i]); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c", ((char *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_SHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned short *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UINT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned int *) buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_LONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_ULONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((unsigned long *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%.8g ", ((float *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%.15g ", ((double *)buf)[i] ); if( CPLStrlcat(szValue, szData, MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } CPLFree( buf ); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose( hAttrID ); poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, osKey, szValue); CPLFree( szData ); CPLFree( szValue ); return 0; }
int gal2gad2(NbodyModel *theModel, const char * prefix, const char * path, double length, double mass, double velocity) { #ifdef HAS_HDF5 int n=theModel->n; hid_t file_id,group_id,header_id; hid_t hdf5_dataspace,hdf5_attribute; hsize_t pdims[2]; hsize_t mdims[1]; hsize_t tdims[1]; hsize_t fdims[1]; float *positions; float *velocities; int *IDs; int nFiles=1; int Npart[6]={0,0,0,0,n,0}; int Npart_hw[6]={0,0,0,0,0,0}; float *masses; herr_t status; int i_zero=0; double d_zero=0.0; double d_one=1.0; int i; char hdf5file[100]; char paramfile[100]; FILE * pfile; sprintf(hdf5file,"%s%s.hdf5",path,prefix); sprintf(paramfile,"%s%s.param",path,prefix); positions = (float *)malloc(sizeof(float)*3*n); velocities = (float *)malloc(sizeof(float)*3*n); masses = (float *)malloc(sizeof(float)*n); IDs = (int *)malloc(sizeof(int)*n); printf("HDF5FILE BEING GENERATED\n"); for(i=0;i<n;i++) { positions[i*3+0] = (float)theModel->x[i]; positions[i*3+1] = (float)theModel->y[i]; positions[i*3+2] = (float)theModel->z[i]; velocities[i*3+0] = (float)theModel->vx[i]; velocities[i*3+1] = (float)theModel->vy[i]; velocities[i*3+2] = (float)theModel->vz[i]; masses[i] = (float)theModel->mass[i]; IDs[i]=i; } file_id = H5Fcreate (hdf5file, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); group_id = H5Gcreate1(file_id, "PartType4", 0); header_id = H5Gcreate1(file_id, "Header", 0); pdims[0] = n; pdims[1] = 3; mdims[0] = n; tdims[0] = 6; fdims[0] = 1; hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate1(header_id, "NumPart_ThisFile", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, Npart); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate1(header_id, "NumPart_Total", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate1(header_id, "NumPart_Total_HW", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Time", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &time); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Redshift", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "BoxSize", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "NumFilesPerSnapshot", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &nFiles); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Omega0", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "OmegaLambda", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "HubbleParam", H5T_NATIVE_DOUBLE, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_DOUBLE, &d_one); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Flag_Sfr", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Flag_Cooling", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Flag_StellarAge", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Flag_Metals", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SCALAR); hdf5_attribute = H5Acreate1(header_id, "Flag_Feedback", H5T_NATIVE_INT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_INT, &i_zero); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); hdf5_dataspace = H5Screate(H5S_SIMPLE); H5Sset_extent_simple(hdf5_dataspace, 1, tdims, NULL); hdf5_attribute = H5Acreate1(header_id, "Flag_Entropy_ICs", H5T_NATIVE_UINT, hdf5_dataspace, H5P_DEFAULT); H5Awrite(hdf5_attribute, H5T_NATIVE_UINT, Npart_hw); H5Aclose(hdf5_attribute); H5Sclose(hdf5_dataspace); status = H5LTmake_dataset(file_id,"PartType4/Coordinates",2, pdims,H5T_NATIVE_FLOAT,positions); status = H5LTmake_dataset(file_id,"PartType4/ParticleIDs",1, mdims,H5T_NATIVE_UINT,IDs); status = H5LTmake_dataset(file_id,"PartType4/Velocities",2, pdims,H5T_NATIVE_FLOAT,velocities); status = H5LTmake_dataset(file_id,"PartType4/Masses",1, mdims,H5T_NATIVE_FLOAT,masses); status = H5Fclose (file_id); free(positions); free(velocities); free(masses); free(IDs); pfile = fopen(paramfile,"w"); fprintf(pfile,"InitCondFile\t%s\n",prefix); fprintf(pfile,"OutputDir\tout\n"); fprintf(pfile,"EnergyFile\tenergy.txt\n"); fprintf(pfile,"InfoFile\tinfo.txt\n"); fprintf(pfile,"TimingsFile\ttimings.txt\n"); fprintf(pfile,"CpuFile\tcpu.txt\n"); fprintf(pfile,"RestartFile\trestart\n"); fprintf(pfile,"SnapshotFileBase\tsnapshot\n"); fprintf(pfile,"OutputListFilename\toutput_list.txt\n"); fprintf(pfile,"ICFormat\t3\n"); fprintf(pfile,"SnapFormat\t3\n"); fprintf(pfile,"TypeOfTimestepCriterion\t0\n"); fprintf(pfile,"OutputListOn\t0\n"); fprintf(pfile,"PeriodicBoundariesOn\t0\n"); fprintf(pfile,"TimeBegin\t0.0\n"); fprintf(pfile,"TimeMax\t%le\n",theModel->tFinal); fprintf(pfile,"Omega0\t0\n"); fprintf(pfile,"OmegaLambda\t0\n"); fprintf(pfile,"OmegaBaryon\t0\n"); fprintf(pfile,"HubbleParam\t1.0\n"); fprintf(pfile,"BoxSize\t0\n"); fprintf(pfile,"TimeBetSnapshot\t%le\n",theModel->tFinal/100); //change this fprintf(pfile,"TimeOfFirstSnapshot\t0\n"); fprintf(pfile,"CpuTimeBetRestartFile\t300.0\n"); fprintf(pfile,"TimeBetStatistics\t0.1\n"); fprintf(pfile,"NumFilesPerSnapshot\t1\n"); fprintf(pfile,"NumFilesWrittenInParallel\t1\n"); fprintf(pfile,"ErrTolIntAccuracy\t0.0025\n"); fprintf(pfile,"CourantFac\t0.15\n"); fprintf(pfile,"MaxSizeTimestep\t0.01\n"); fprintf(pfile,"MinSizeTimestep\t0.0\n"); fprintf(pfile,"ErrTolTheta\t0.05\n"); fprintf(pfile,"TypeOfOpeningCriterion\t1\n"); fprintf(pfile,"ErrTolForceAcc\t0.0005\n"); fprintf(pfile,"TreeDomainUpdateFrequency\t0.01\n"); fprintf(pfile,"DesNumNgb\t50\n"); fprintf(pfile,"MaxNumNgbDeviation\t2\n"); fprintf(pfile,"ArtBulkViscConst\t0.8\n"); fprintf(pfile,"InitGasTemp\t0\n"); fprintf(pfile,"MinGasTemp\t0\n"); fprintf(pfile,"PartAllocFactor\t1.5\n"); fprintf(pfile,"TreeAllocFactor\t0.8\n"); fprintf(pfile,"BufferSize\t25\n"); fprintf(pfile,"UnitLength_in_cm\t%le \n",length); fprintf(pfile,"UnitMass_in_g\t%le \n",mass); fprintf(pfile,"UnitVelocity_in_cm_per_s\t%le \n",velocity); fprintf(pfile,"GravityConstantInternal\t0\n"); fprintf(pfile,"MinGasHsmlFractional\t0.25\n"); fprintf(pfile,"SofteningGas\t0\n"); fprintf(pfile,"SofteningHalo\t1.0\n"); fprintf(pfile,"SofteningDisk\t0.4\n"); fprintf(pfile,"SofteningBulge\t0\n"); fprintf(pfile,"SofteningStars\t1.0e-2\n"); fprintf(pfile,"SofteningBndry\t0\n"); fprintf(pfile,"SofteningGasMaxPhys\t0\n"); fprintf(pfile,"SofteningHaloMaxPhys\t1.0\n"); fprintf(pfile,"SofteningDiskMaxPhys\t0.4\n"); fprintf(pfile,"SofteningBulgeMaxPhys\t0\n"); fprintf(pfile,"SofteningStarsMaxPhys\t1.0e-2\n"); fprintf(pfile,"SofteningBndryMaxPhys\t0\n"); fprintf(pfile,"MaxRMSDisplacementFac\t0.2\n"); fprintf(pfile,"TimeLimitCPU\t36000\n"); fprintf(pfile,"ResubmitOn\t0\n"); fprintf(pfile,"ResubmitCommand\tmy-scriptfile\n"); fprintf(pfile,"ComovingIntegrationOn\t0\n"); fclose(pfile); return 0; #else printf("ATTEMTPING TO RUN GAL2GAD2 ROUTINE WITHOUT HDF5 SUPPORT!\n"); printf("EXITING!\n"); exit(0); return 1; #endif }
int getBaxData(BaxData *b, char *fname) { hid_t field_space; hid_t field_set; hsize_t field_len[2]; hid_t file_id; herr_t stat; int ecode; hid_t type; hid_t attr; char *name; H5Eset_auto(H5E_DEFAULT,0,0); // silence hdf5 error stack file_id = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); if (file_id < 0) return (CANNOT_OPEN_BAX_FILE); #ifdef DEBUG printf("PROCESSING %s, file_id: %d\n", baxFileName, file_id); #endif #define GET_SIZE(path,error) \ { ecode = error; \ if ((field_set = H5Dopen2(file_id, path, H5P_DEFAULT)) < 0) goto exit0; \ if ((field_space = H5Dget_space(field_set)) < 0) goto exit1; \ H5Sget_simple_extent_dims(field_space, field_len, NULL); \ } #define FETCH(field,type) \ { stat = H5Dread(field_set, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, b->field); \ H5Sclose(field_space); \ H5Dclose(field_set); \ if (stat < 0) goto exit0; \ } #define CHECK_FETCH(path,error,field,type,cntr) \ { GET_SIZE(path,error) \ if (b->cntr != field_len[0]) goto exit2; \ FETCH(field,type) \ } ecode = BAX_MOVIENAME_ERR; if ((field_set = H5Gopen2(file_id,"/ScanData/RunInfo",H5P_DEFAULT)) < 0) goto exit0; if ((attr = H5Aopen(field_set,"MovieName",H5P_DEFAULT)) < 0) goto exit3; if ((field_space = H5Aget_space(attr)) < 0) goto exit4; if ((type = H5Aget_type(attr)) < 0) goto exit5; H5Aread(attr,type,&name); ensureMovie(b,strlen(name)); strcpy(b->movieName,name); H5Tclose(type); H5Sclose(field_space); H5Aclose(attr); H5Gclose(field_set); GET_SIZE("/PulseData/BaseCalls/Basecall",BAX_BASECALL_ERR) ensureBases(b,field_len[0]); FETCH(baseCall,H5T_NATIVE_UCHAR) if (b->arrow) CHECK_FETCH("/PulseData/BaseCalls/WidthInFrames",BAX_PULSE_ERR,pulseW,H5T_NATIVE_USHORT,numBP) if (b->fastq) CHECK_FETCH("/PulseData/BaseCalls/QualityValue",BAX_QV_ERR,fastQV,H5T_NATIVE_UCHAR,numBP) if (b->quivqv) { CHECK_FETCH("/PulseData/BaseCalls/DeletionQV", BAX_DEL_ERR,delQV, H5T_NATIVE_UCHAR,numBP) CHECK_FETCH("/PulseData/BaseCalls/DeletionTag", BAX_TAG_ERR,delTag, H5T_NATIVE_UCHAR,numBP) CHECK_FETCH("/PulseData/BaseCalls/InsertionQV", BAX_INS_ERR,insQV, H5T_NATIVE_UCHAR,numBP) CHECK_FETCH("/PulseData/BaseCalls/MergeQV", BAX_MRG_ERR,mergeQV,H5T_NATIVE_UCHAR,numBP) CHECK_FETCH("/PulseData/BaseCalls/SubstitutionQV",BAX_SUB_ERR,subQV, H5T_NATIVE_UCHAR,numBP) } GET_SIZE("/PulseData/BaseCalls/ZMW/HoleStatus",BAX_HOLESTATUS_ERR) ensureZMW(b,field_len[0]); FETCH(holeType,H5T_NATIVE_UCHAR) CHECK_FETCH("/PulseData/BaseCalls/ZMW/NumEvent",BAX_NR_EVENTS_ERR,readLen,H5T_NATIVE_INT,numZMW) if (b->arrow) { CHECK_FETCH("/PulseData/BaseCalls/ZMWMetrics/HQRegionSNR",BAX_SNR_ERR,snrVec, H5T_NATIVE_FLOAT,numZMW) ecode = BAX_CHIPSET_ERR; if ((field_set = H5Gopen2(file_id,"/ScanData/DyeSet",H5P_DEFAULT)) < 0) goto exit0; if ((attr = H5Aopen(field_set,"BaseMap",H5P_DEFAULT)) < 0) goto exit3; if ((field_space = H5Aget_space(attr)) < 0) goto exit4; if ((type = H5Aget_type(attr)) < 0) goto exit5; H5Aread(attr,type,&name); { int i; for (i = 0; i < 4; i++) b->chan[i] = DNA_2_NUMBER[(int) name[i]]; } H5Tclose(type); H5Sclose(field_space); H5Aclose(attr); H5Gclose(field_set); } GET_SIZE("/PulseData/Regions",BAX_REGION_ERR) ensureHQR(b,field_len[0]); FETCH(regions,H5T_NATIVE_INT) // Find the Del QV associated with N's in the Del Tag if (b->quivqv) { hsize_t i; for (i = 0; i < b->numBP; i++) if (b->delTag[i] == 'N') { b->delLimit = b->delQV[i]; break; } } H5Fclose(file_id); return (0); exit5: H5Sclose(field_space); exit4: H5Aclose(attr); exit3: H5Gclose(field_set); H5Fclose(file_id); return (ecode); exit2: H5Sclose(field_space); exit1: H5Dclose(field_set); exit0: H5Fclose(file_id); return (ecode); }