// * * * * * * * * * * * * * * * * * * * * * * * * * * void H5_C3PO_NS::createExtendibleDataset(std::string FILE_NAME,const char* datasetName_) { hsize_t dims[2] = { 0, 1}; // dataset dimensions at creation hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED}; DataSpace mspace1( RANK, dims, maxdims); H5File* file=new H5File( FILE_NAME.c_str(),H5F_ACC_RDWR ); IntType datatype( PredType::NATIVE_DOUBLE ); //Define datatype for the data datatype.setOrder( H5T_ORDER_LE ); DSetCreatPropList cparms; hsize_t chunk_dims[2] ={6, 1}; cparms.setChunk( RANK, chunk_dims ); //Set fill value for the dataset int fill_val = 1.0; cparms.setFillValue( PredType::NATIVE_DOUBLE, &fill_val); DataSet dataset = file->createDataSet( datasetName_, PredType::NATIVE_DOUBLE, mspace1, cparms); file->close(); delete file; }
/*------------------------------------------------------------------------- * Function: test_vlstrings_special * * Purpose: Test VL string code for special string cases, nil and * zero-sized. * * Return: None * * Programmer: Binh-Minh Ribler (use C version) * January, 2007 * *------------------------------------------------------------------------- */ static void test_vlstrings_special() { const char *wdata[SPACE1_DIM1] = {"one", "two", "", "four"}; const char *wdata2[SPACE1_DIM1] = {NULL, NULL, NULL, NULL}; char *rdata[SPACE1_DIM1]; // Information read in // Output message about test being performed. SUBTEST("Special VL Strings"); try { // Create file. H5File file1(FILENAME, H5F_ACC_TRUNC); // Create dataspace for datasets. hsize_t dims1[] = {SPACE1_DIM1}; DataSpace sid1(SPACE1_RANK, dims1); // Create a datatype to refer to. StrType vlst(0, H5T_VARIABLE); // Create a dataset. DataSet dataset(file1.createDataSet("Dataset3", vlst, sid1)); // Read from the dataset before writing data. dataset.read(rdata, vlst); // Check data read in. hsize_t i; // counting variable for (i=0; i<SPACE1_DIM1; i++) if(rdata[i]!=NULL) TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]); // Write dataset to disk, then read it back. dataset.write(wdata, vlst); dataset.read(rdata, vlst); // Compare data read in. for (i = 0; i < SPACE1_DIM1; i++) { size_t wlen = HDstrlen(wdata[i]); size_t rlen = HDstrlen(rdata[i]); if(wlen != rlen) { TestErrPrintf("VL data lengths don't match!, strlen(wdata[%d])=%u, strlen(rdata[%d])=%u\n", (int)i, (unsigned)wlen, (int)i, (unsigned)rlen); continue; } // end if if(HDstrcmp(wdata[i],rdata[i]) != 0) { TestErrPrintf("VL data values don't match!, wdata[%d]=%s, rdata[%d]=%s\n", (int)i, wdata[i], (int)i, rdata[i]); continue; } // end if } // end for // Reclaim the read VL data. DataSet::vlenReclaim((void *)rdata, vlst, sid1); // Close Dataset. dataset.close(); /* * Create another dataset to test nil strings. */ // Create the property list and set the fill value for the second // dataset. DSetCreatPropList dcpl; char *fill = NULL; // Fill value dcpl.setFillValue(vlst, &fill); dataset = file1.createDataSet("Dataset4", vlst, sid1, dcpl); // Close dataset creation property list. dcpl.close(); // Read from dataset before writing data. dataset.read(rdata, vlst); // Check data read in. for (i=0; i<SPACE1_DIM1; i++) if(rdata[i]!=NULL) TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]); // Try to write nil strings to disk. dataset.write(wdata2, vlst); // Read nil strings back from disk. dataset.read(rdata, vlst); // Check data read in. for (i=0; i<SPACE1_DIM1; i++) if(rdata[i]!=NULL) TestErrPrintf("VL doesn't match!, rdata[%d]=%p\n",(int)i,rdata[i]); // Close objects and file. dataset.close(); vlst.close(); sid1.close(); file1.close(); PASSED(); } // end try // Catch all exceptions. catch (Exception E) { issue_fail_msg("test_vlstrings_special()", __LINE__, __FILE__, E.getCDetailMsg()); } } // test_vlstrings_special
/* * Create data spaces and data sets of the hdf5 for recording histories. */ void Hdf5Recorder::initDataSet() { // create the data space & dataset for burstiness history hsize_t dims[2]; dims[0] = static_cast<hsize_t>(m_sim_info->epochDuration * m_sim_info->maxSteps); DataSpace dsBurstHist(1, dims); dataSetBurstHist = new DataSet(stateOut->createDataSet(nameBurstHist, PredType::NATIVE_INT, dsBurstHist)); // create the data space & dataset for spikes history dims[0] = static_cast<hsize_t>(m_sim_info->epochDuration * m_sim_info->maxSteps * 100); DataSpace dsSpikesHist(1, dims); dataSetSpikesHist = new DataSet(stateOut->createDataSet(nameSpikesHist, PredType::NATIVE_INT, dsSpikesHist)); // create the data space & dataset for xloc & ylo c dims[0] = static_cast<hsize_t>(m_sim_info->totalNeurons); DataSpace dsXYloc(1, dims); dataSetXloc = new DataSet(stateOut->createDataSet(nameXloc, PredType::NATIVE_INT, dsXYloc)); dataSetYloc = new DataSet(stateOut->createDataSet(nameYloc, PredType::NATIVE_INT, dsXYloc)); // create the data space & dataset for neuron types dims[0] = static_cast<hsize_t>(m_sim_info->totalNeurons); DataSpace dsNeuronTypes(1, dims); dataSetNeuronTypes = new DataSet(stateOut->createDataSet(nameNeuronTypes, PredType::NATIVE_INT, dsNeuronTypes)); // create the data space & dataset for neuron threashold dims[0] = static_cast<hsize_t>(m_sim_info->totalNeurons); DataSpace dsNeuronThresh(1, dims); dataSetNeuronThresh = new DataSet(stateOut->createDataSet(nameNeuronThresh, H5_FLOAT, dsNeuronThresh)); // create the data space & dataset for simulation step duration dims[0] = static_cast<hsize_t>(1); DataSpace dsTsim(1, dims); dataSetTsim = new DataSet(stateOut->createDataSet(nameTsim, H5_FLOAT, dsTsim)); // create the data space & dataset for simulation end time dims[0] = static_cast<hsize_t>(1); DataSpace dsSimulationEndTime(1, dims); dataSetSimulationEndTime = new DataSet(stateOut->createDataSet(nameSimulationEndTime, H5_FLOAT, dsSimulationEndTime)); // probed neurons if (m_model->getLayout()->m_probed_neuron_list.size() > 0) { // create the data space & dataset for probed neurons dims[0] = static_cast<hsize_t>(m_model->getLayout()->m_probed_neuron_list.size()); DataSpace dsProbedNeurons(1, dims); dataSetProbedNeurons = new DataSet(stateOut->createDataSet(nameProbedNeurons, PredType::NATIVE_INT, dsProbedNeurons)); // create the data space & dataset for spikes of probed neurons // the data space with unlimited dimensions hsize_t maxdims[2]; maxdims[0] = H5S_UNLIMITED; maxdims[1] = static_cast<hsize_t>(m_model->getLayout()->m_probed_neuron_list.size()); // dataset dimensions at creation dims[0] = static_cast<hsize_t>(1); dims[1] = static_cast<hsize_t>(m_model->getLayout()->m_probed_neuron_list.size()); DataSpace dsSpikesProbedNeurons(2, dims, maxdims); // set fill value for the dataset DSetCreatPropList cparms; uint64_t fill_val = 0; cparms.setFillValue( PredType::NATIVE_UINT64, &fill_val); // modify dataset creation properties, enable chunking hsize_t chunk_dims[2]; chunk_dims[0] = static_cast<hsize_t>(100); chunk_dims[1] = static_cast<hsize_t>(m_model->getLayout()->m_probed_neuron_list.size()); cparms.setChunk( 2, chunk_dims ); dataSetSpikesProbedNeurons = new DataSet(stateOut->createDataSet(nameSpikesProbedNeurons, PredType::NATIVE_UINT64, dsSpikesProbedNeurons, cparms)); } // allocate data memories burstinessHist = new int[static_cast<int>(m_sim_info->epochDuration)]; spikesHistory = new int[static_cast<int>(m_sim_info->epochDuration * 100)]; memset(burstinessHist, 0, static_cast<int>(m_sim_info->epochDuration * sizeof(int))); memset(spikesHistory, 0, static_cast<int>(m_sim_info->epochDuration * 100 * sizeof(int))); // create the data space & dataset for spikes history of probed neurons if (m_model->getLayout()->m_probed_neuron_list.size() > 0) { // allocate data for spikesProbedNeurons spikesProbedNeurons = new vector<uint64_t>[m_model->getLayout()->m_probed_neuron_list.size()]; // allocate memory to save offset offsetSpikesProbedNeurons = new hsize_t[m_model->getLayout()->m_probed_neuron_list.size()]; memset(offsetSpikesProbedNeurons, 0, static_cast<int>(m_model->getLayout()->m_probed_neuron_list.size() * sizeof(hsize_t))); } }
int main (void) { int i,j; // loop indices */ /* * Try block to detect exceptions raised by any of the calls inside it */ try { /* * Turn off the auto-printing when failure occurs so that we can * handle the errors appropriately */ Exception::dontPrint(); /* * Create a file. */ H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC ); /* * Create property list for a dataset and set up fill values. */ int fillvalue = 0; /* Fill value for the dataset */ DSetCreatPropList plist; plist.setFillValue(PredType::NATIVE_INT, &fillvalue); /* * Create dataspace for the dataset in the file. */ hsize_t fdim[] = {FSPACE_DIM1, FSPACE_DIM2}; // dim sizes of ds (on disk) DataSpace fspace( FSPACE_RANK, fdim ); /* * Create dataset and write it into the file. */ DataSet* dataset = new DataSet(file->createDataSet( DATASET_NAME, PredType::NATIVE_INT, fspace, plist)); /* * Select hyperslab for the dataset in the file, using 3x2 blocks, * (4,3) stride and (2,4) count starting at the position (0,1). */ hsize_t start[2]; // Start of hyperslab hsize_t stride[2]; // Stride of hyperslab hsize_t count[2]; // Block count hsize_t block[2]; // Block sizes start[0] = 0; start[1] = 1; stride[0] = 4; stride[1] = 3; count[0] = 2; count[1] = 4; block[0] = 3; block[1] = 2; fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block); /* * Create dataspace for the first dataset. */ hsize_t dim1[] = {MSPACE1_DIM}; /* Dimension size of the first dataset (in memory) */ DataSpace mspace1( MSPACE1_RANK, dim1 ); /* * Select hyperslab. * We will use 48 elements of the vector buffer starting at the * second element. Selected elements are 1 2 3 . . . 48 */ start[0] = 1; stride[0] = 1; count[0] = 48; block[0] = 1; mspace1.selectHyperslab( H5S_SELECT_SET, count, start, stride, block); /* * Write selection from the vector buffer to the dataset in the file. * * File dataset should look like this: * 0 1 2 0 3 4 0 5 6 0 7 8 * 0 9 10 0 11 12 0 13 14 0 15 16 * 0 17 18 0 19 20 0 21 22 0 23 24 * 0 0 0 0 0 0 0 0 0 0 0 0 * 0 25 26 0 27 28 0 29 30 0 31 32 * 0 33 34 0 35 36 0 37 38 0 39 40 * 0 41 42 0 43 44 0 45 46 0 47 48 * 0 0 0 0 0 0 0 0 0 0 0 0 */ int vector[MSPACE1_DIM]; // vector buffer for dset /* * Buffer initialization. */ vector[0] = vector[MSPACE1_DIM - 1] = -1; for (i = 1; i < MSPACE1_DIM - 1; i++) vector[i] = i; dataset->write( vector, PredType::NATIVE_INT, mspace1, fspace ); /* * Reset the selection for the file dataspace fid. */ fspace.selectNone(); /* * Create dataspace for the second dataset. */ hsize_t dim2[] = {MSPACE2_DIM}; /* Dimension size of the second dataset (in memory */ DataSpace mspace2( MSPACE2_RANK, dim2 ); /* * Select sequence of NPOINTS points in the file dataspace. */ hsize_t coord[NPOINTS][FSPACE_RANK]; /* Array to store selected points from the file dataspace */ coord[0][0] = 0; coord[0][1] = 0; coord[1][0] = 3; coord[1][1] = 3; coord[2][0] = 3; coord[2][1] = 5; coord[3][0] = 5; coord[3][1] = 6; fspace.selectElements( H5S_SELECT_SET, NPOINTS, (const hsize_t *)coord); /* * Write new selection of points to the dataset. */ int values[] = {53, 59, 61, 67}; /* New values to be written */ dataset->write( values, PredType::NATIVE_INT, mspace2, fspace ); /* * File dataset should look like this: * 53 1 2 0 3 4 0 5 6 0 7 8 * 0 9 10 0 11 12 0 13 14 0 15 16 * 0 17 18 0 19 20 0 21 22 0 23 24 * 0 0 0 59 0 61 0 0 0 0 0 0 * 0 25 26 0 27 28 0 29 30 0 31 32 * 0 33 34 0 35 36 67 37 38 0 39 40 * 0 41 42 0 43 44 0 45 46 0 47 48 * 0 0 0 0 0 0 0 0 0 0 0 0 * */ /* * Close the dataset and the file. */ delete dataset; delete file; /* * Open the file. */ file = new H5File( FILE_NAME, H5F_ACC_RDONLY ); /* * Open the dataset. */ dataset = new DataSet( file->openDataSet( DATASET_NAME )); /* * Get dataspace of the dataset. */ fspace = dataset->getSpace(); /* * Select first hyperslab for the dataset in the file. The following * elements are selected: * 10 0 11 12 * 18 0 19 20 * 0 59 0 61 * */ start[0] = 1; start[1] = 2; block[0] = 1; block[1] = 1; stride[0] = 1; stride[1] = 1; count[0] = 3; count[1] = 4; fspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block); /* * Add second selected hyperslab to the selection. * The following elements are selected: * 19 20 0 21 22 * 0 61 0 0 0 * 27 28 0 29 30 * 35 36 67 37 38 * 43 44 0 45 46 * 0 0 0 0 0 * Note that two hyperslabs overlap. Common elements are: * 19 20 * 0 61 */ start[0] = 2; start[1] = 4; block[0] = 1; block[1] = 1; stride[0] = 1; stride[1] = 1; count[0] = 6; count[1] = 5; fspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block); /* * Create memory dataspace. */ hsize_t mdim[] = {MSPACE_DIM1, MSPACE_DIM2}; /* Dimension sizes of the dataset in memory when we read selection from the dataset on the disk */ DataSpace mspace(MSPACE_RANK, mdim); /* * Select two hyperslabs in memory. Hyperslabs has the same * size and shape as the selected hyperslabs for the file dataspace. */ start[0] = 0; start[1] = 0; block[0] = 1; block[1] = 1; stride[0] = 1; stride[1] = 1; count[0] = 3; count[1] = 4; mspace.selectHyperslab(H5S_SELECT_SET, count, start, stride, block); start[0] = 1; start[1] = 2; block[0] = 1; block[1] = 1; stride[0] = 1; stride[1] = 1; count[0] = 6; count[1] = 5; mspace.selectHyperslab(H5S_SELECT_OR, count, start, stride, block); /* * Initialize data buffer. */ int matrix_out[MSPACE_DIM1][MSPACE_DIM2]; for (i = 0; i < MSPACE_DIM1; i++) for (j = 0; j < MSPACE_DIM2; j++) matrix_out[i][j] = 0; /* * Read data back to the buffer matrix. */ dataset->read(matrix_out, PredType::NATIVE_INT, mspace, fspace); /* * Display the result. Memory dataset is: * * 10 0 11 12 0 0 0 0 0 * 18 0 19 20 0 21 22 0 0 * 0 59 0 61 0 0 0 0 0 * 0 0 27 28 0 29 30 0 0 * 0 0 35 36 67 37 38 0 0 * 0 0 43 44 0 45 46 0 0 * 0 0 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 0 0 */ for (i=0; i < MSPACE_DIM1; i++) { for(j=0; j < MSPACE_DIM2; j++) cout << matrix_out[i][j] << " "; cout << endl; } /* * Close the dataset and the file. */ delete dataset; delete file; } // end of try block // catch failure caused by the H5File operations catch( FileIException error ) { error.printErrorStack(); return -1; } // catch failure caused by the DataSet operations catch( DataSetIException error ) { error.printErrorStack(); return -1; } // catch failure caused by the DataSpace operations catch( DataSpaceIException error ) { error.printErrorStack(); return -1; } return 0; }
void Generic_wrapper_hdf::add_dset(int rank, const unsigned int * dims, V_TYPE type, const void * data, const std::string & name ) { if (!(wrapper_open_)) throw runtime_error("wrapper must be open to add a dataset"); hsize_t hdims[rank]; for(int j = 0;j<rank;++j) hdims[j] = dims[j]; // make dspace DataSpace dspace(rank,hdims); // sort out type DataType hdf_type,mem_type; DSetCreatPropList plist; int fill_value_i = -31415; unsigned int fill_value_ui = 0; float fill_value_f = -3.1415; switch(type) { case V_INT: hdf_type = PredType::NATIVE_INT; mem_type = PredType::NATIVE_INT; plist.setFillValue(hdf_type,&fill_value_i); break; case V_FLOAT: hdf_type = PredType::NATIVE_FLOAT; mem_type = PredType::NATIVE_FLOAT; plist.setFillValue(hdf_type,&fill_value_f); break; case V_UINT: hdf_type = PredType::NATIVE_UINT; mem_type = PredType::NATIVE_UINT; plist.setFillValue(hdf_type,&fill_value_ui); break; case V_BOOL: case V_TIME: case V_GUID: case V_ERROR: case V_COMPLEX: case V_STRING: throw logic_error("generic_wrapper_hdf: un implemented types"); } /// @todo add compression logic for higher sizes // if the list is big enough, us compression if(rank ==1 && *hdims > CSIZE*5) { hsize_t csize = CSIZE; plist.setChunk(1,&csize); plist.setSzip(H5_SZIP_NN_OPTION_MASK,10); } // make data set DataSet dset; if(!group_open_ || name[0] == '/') { dset = file_ ->createDataSet(name,hdf_type,dspace,plist); } else if(group_) { dset = group_ ->createDataSet(name,hdf_type,dspace,plist); } else { throw runtime_error("gave relative path name with no open group"); } // shove in data dset.write(data,mem_type,dspace,dspace); // close everything is taken care of as all variables on stack }
int main (void) { /* * Try block to detect exceptions raised by any of the calls inside it */ try { /* * Turn off the auto-printing when failure occurs so that we can * handle the errors appropriately */ Exception::dontPrint(); /* * Create the data space with unlimited dimensions. */ hsize_t dims[2] = { 3, 3}; // dataset dimensions at creation hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED}; DataSpace mspace1( RANK, dims, maxdims); /* * Create a new file. If file exists its contents will be overwritten. */ H5File file( FILE_NAME, H5F_ACC_TRUNC ); /* * Modify dataset creation properties, i.e. enable chunking. */ DSetCreatPropList cparms; hsize_t chunk_dims[2] ={2, 5}; cparms.setChunk( RANK, chunk_dims ); /* * Set fill value for the dataset */ int fill_val = 0; cparms.setFillValue( PredType::NATIVE_INT, &fill_val); /* * Create a new dataset within the file using cparms * creation properties. */ DataSet dataset = file.createDataSet( DATASET_NAME, PredType::NATIVE_INT, mspace1, cparms); /* * Extend the dataset. This call assures that dataset is at least 3 x 3. */ hsize_t size[2]; size[0] = 3; size[1] = 3; dataset.extend( size ); /* * Select a hyperslab. */ DataSpace fspace1 = dataset.getSpace (); hsize_t offset[2]; offset[0] = 0; offset[1] = 0; hsize_t dims1[2] = { 3, 3}; /* data1 dimensions */ fspace1.selectHyperslab( H5S_SELECT_SET, dims1, offset ); /* * Write the data to the hyperslab. */ int data1[3][3] = { {1, 1, 1}, /* data to write */ {1, 1, 1}, {1, 1, 1} }; dataset.write( data1, PredType::NATIVE_INT, mspace1, fspace1 ); /* * Extend the dataset. Dataset becomes 10 x 3. */ hsize_t dims2[2] = { 7, 1}; /* data2 dimensions */ dims[0] = dims1[0] + dims2[0]; size[0] = dims[0]; size[1] = dims[1]; dataset.extend( size ); /* * Select a hyperslab. */ DataSpace fspace2 = dataset.getSpace (); offset[0] = 3; offset[1] = 0; fspace2.selectHyperslab( H5S_SELECT_SET, dims2, offset ); /* * Define memory space */ DataSpace mspace2( RANK, dims2 ); /* * Write the data to the hyperslab. */ int data2[7] = { 2, 2, 2, 2, 2, 2, 2}; dataset.write( data2, PredType::NATIVE_INT, mspace2, fspace2 ); /* * Extend the dataset. Dataset becomes 10 x 5. */ hsize_t dims3[2] = { 2, 2}; /* data3 dimensions */ dims[1] = dims1[1] + dims3[1]; size[0] = dims[0]; size[1] = dims[1]; dataset.extend( size ); /* * Select a hyperslab */ DataSpace fspace3 = dataset.getSpace (); offset[0] = 0; offset[1] = 3; fspace3.selectHyperslab( H5S_SELECT_SET, dims3, offset ); /* * Define memory space. */ DataSpace mspace3( RANK, dims3 ); /* * Write the data to the hyperslab. */ int data3[2][2] = { {3, 3}, {3, 3} }; dataset.write( data3, PredType::NATIVE_INT, mspace3, fspace3 ); /* * Read the data from this dataset and display it. */ int i, j; int data_out[NX][NY]; for (i = 0; i < NX; i++) { for (j = 0; j < NY; j++) data_out[i][j] = 0; } dataset.read( data_out, PredType::NATIVE_INT ); /* * Resulting dataset * * 1 1 1 3 3 * 1 1 1 3 3 * 1 1 1 0 0 * 2 0 0 0 0 * 2 0 0 0 0 * 2 0 0 0 0 * 2 0 0 0 0 * 2 0 0 0 0 * 2 0 0 0 0 * 2 0 0 0 0 */ /* * Display the result. */ for (i=0; i < NX; i++) { for(j=0; j < NY; j++) cout << data_out[i][j] << " "; cout << endl; } } // end of try block // catch failure caused by the H5File operations catch( FileIException error ) { error.printErrorStack(); return -1; } // catch failure caused by the DataSet operations catch( DataSetIException error ) { error.printErrorStack(); return -1; } // catch failure caused by the DataSpace operations catch( DataSpaceIException error ) { error.printErrorStack(); return -1; } // catch failure caused by the DataSpace operations catch( DataTypeIException error ) { error.printErrorStack(); return -1; } return 0; }