我正在尝试使用复合数据类型并行编写一组变量(dep_var类的gridinfo对象)。我的总系统大小是NX x NY x NZ,工作人员信息存储在worker(worker_info类的obj)中。函数“write_hdf(...)”仅由工人执行。
现在,我的gridinfo是形式为(i + offset_x)Ny * Nz +(k + offset_z)* Ny +(j + offset_y)的一维对象数组。我在末尾有填充,由offset_x,offset_y和offset_z指定。当我希望我的数据空间是一个3D数组时,我无法理解如何在memspace中为1D数组指定此偏移量。请注意,main_offset_x,y,z是数据空间上工作块的偏移量。
使用hdf教程,我能够写到这一点。如果你能提供一些关于如何前进的提示,我将不胜感激。在此先感谢!!
void write_hdf(MPI_Comm comm,MPI_Func *mpi_obj,int iter){
stringstream hdf_fname;
hdf_fname<<"Out_"<<iter<<".h5";
string hdf_filename = hdf_fname.str();
//Create a new file collectively and release property list identifier.
hid_t fapl_id=H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(fapl_id,comm,MPI_INFO_NULL);
hid_t file_id=H5Fcreate(hdf_filename.c_str(),H5F_ACC_TRUNC,H5P_DEFAULT,fapl_id);
H5Pclose(fapl_id);
int RANK=3;
hsize_t dims[RANK];
herr_t status;
dims[0]=NX;dims[1]=NY;dims[2]=NZ;
//Create dataspace
hid_t dataspace_id=H5Screate_simple(RANK,dims,NULL);
//Create compound datatype
hid_t cmpd_dtype_id=H5Tcreate(H5T_COMPOUND,sizeof(dep_var));
status=H5Tinsert(cmpd_dtype_id, "Temperature", HOFFSET(dep_var,Temp),H5T_NATIVE_DOUBLE);
status=H5Tinsert(cmpd_dtype_id, "fs", HOFFSET(dep_var,fs),H5T_NATIVE_FLOAT);
status = H5Tset_order(cmpd_dtype_id, H5T_ORDER_LE);
//Create chunk dataset
dims[0]=worker->Nx;dims[1]=worker->Ny;dims[2]=worker->Nz;
hid_t plist_id = H5Pcreate(H5P_DATASET_CREATE);
H5Pset_chunk(plist_id, RANK, dims);
hid_t dataset_id=H5Dcreate(file_id,"All_VAR",cmpd_dtype_id,dataspace_id,H5P_DEFAULT,plist_id,H5P_DEFAULT);
H5Pclose(plist_id);
H5Sclose(dataspace_id);
//create memspace for each worker
hsize_t offset[RANK],block[RANK],stride[RANK],count[RANK];
hid_t memspace_id=H5Screate_simple(RANK,dims,NULL);
count[0] =worker->Nx;count[1] = worker->Ny ;count[2]=worker->Nz;
stride[0] = 1;stride[1] = 1;stride[2]=1;
block[0] = dims[0];block[1] = dims[1];block[2]=dims[2];
offset[0]=worker->main_offset_x;
offset[1]=worker->main_offset_y;
offset[2]=worker->main_offset_z;
dataspace_id = H5Dget_space(dataset_id);
status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, stride, count, block);
//Create property list for collective dataset write.
hid_t xf_id=H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(xf_id,H5FD_MPIO_COLLECTIVE);
//Write File
status = H5Dwrite(dataset_id, cmpd_dtype_id, memspace_id, dataspace_id,xf_id, &(gridinfo[0]));
//Free
H5Sclose(dataspace_id);
H5Sclose(memspace_id);
H5Dclose(dataset_id);
H5Pclose(xf_id);
H5Fclose(file_id);
}
class dep_var{
public:
double Temp;
double fs;
};
class worker_info{
public:
int Nx,Ny,Nz;//Worker size
int real_Nx,real_Ny,real_Nz;//Size with padding
int offset_x,offset_y,offset_z; //Offsets for padding
int main_offset_x,main_offset_y,main_offset_z; //offsets for this worker on dataspace
};
答案 0 :(得分:1)
It is all about indexing, we can regard 3D array as a "book" which has NZ pages with each page NX(column)*NY(row) words. We now numbering words in this "book” row by row.
Then the index of a word may be written as:
index=NX*NY*z+y*NX+x;
问候。