static ssize_t sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct sysfs_buffer * buffer = file->private_data; count = fill_write_buffer(buffer,buf,count); if (count > 0) count = flush_write_buffer(file,buffer,count); if (count > 0) *ppos += count; return count; }
static ssize_t configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct configfs_buffer * buffer = file->private_data; ssize_t len; mutex_lock(&buffer->mutex); len = fill_write_buffer(buffer, buf, count); if (len > 0) len = flush_write_buffer(file->f_path.dentry, buffer, len); if (len > 0) *ppos += len; mutex_unlock(&buffer->mutex); return len; }
static ssize_t sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct sysfs_buffer * buffer = file->private_data; ssize_t len; down(&buffer->sem); len = fill_write_buffer(buffer, buf, count); if (len > 0) len = flush_write_buffer(file->f_path.dentry, buffer, len); if (len > 0) *ppos += len; up(&buffer->sem); return len; }
static ssize_t configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct configfs_buffer *buffer = file->private_data; struct configfs_dirent *sd = file->f_path.dentry->d_fsdata; ssize_t len; if (WARN_ON(sd == NULL)) return -EINVAL; if (WARN_ON(!(sd->s_type & CONFIGFS_ITEM_ATTR))) return -EINVAL; mutex_lock(&buffer->mutex); len = fill_write_buffer(buffer, buf, count); if (len > 0) len = flush_write_buffer(file->f_path.dentry, buffer, len); if (len > 0) *ppos += len; mutex_unlock(&buffer->mutex); return len; }
/*! This function writes an actual snapshot file containing the data from * processors 'writeTask' to 'lastTask'. 'writeTask' is the one that actually * writes. Each snapshot file contains a header first, then particle * positions, velocities and ID's. Particle masses are written only for * those particle types with zero entry in MassTable. After that, first the * internal energies u, and then the density is written for the SPH * particles. If cooling is enabled, mean molecular weight and neutral * hydrogen abundance are written for the gas particles. This is followed by * the SPH smoothing length and further blocks of information, depending on * included physics and compile-time flags. If HDF5 is used, the header is * stored in a group called "/Header", and the particle data is stored * separately for each particle type in groups calles "/PartType0", * "/PartType1", etc. The sequence of the blocks is unimportant in this case. */ void write_file(char *fname, int writeTask, int lastTask) { int type, bytes_per_blockelement, npart, nextblock, typelist[6]; int n_for_this_task, ntask, n, p, pc, offset = 0, task; int blockmaxlen, ntot_type[6], nn[6]; enum iofields blocknr; int blksize; MPI_Status status; FILE *fd = 0; #ifdef HAVE_HDF5 hid_t hdf5_file = 0, hdf5_grp[6], hdf5_headergrp = 0, hdf5_dataspace_memory; hid_t hdf5_datatype = 0, hdf5_dataspace_in_file = 0, hdf5_dataset = 0; herr_t hdf5_status; hsize_t dims[2], count[2], start[2]; int rank, pcsum = 0; char buf[500]; #endif #define SKIP {my_fwrite(&blksize,sizeof(int),1,fd);} /* determine particle numbers of each type in file */ if(ThisTask == writeTask) { for(n = 0; n < 6; n++) ntot_type[n] = n_type[n]; for(task = writeTask + 1; task <= lastTask; task++) { MPI_Recv(&nn[0], 6, MPI_INT, task, TAG_LOCALN, MPI_COMM_WORLD, &status); for(n = 0; n < 6; n++) ntot_type[n] += nn[n]; } for(task = writeTask + 1; task <= lastTask; task++) MPI_Send(&ntot_type[0], 6, MPI_INT, task, TAG_N, MPI_COMM_WORLD); } else { MPI_Send(&n_type[0], 6, MPI_INT, writeTask, TAG_LOCALN, MPI_COMM_WORLD); MPI_Recv(&ntot_type[0], 6, MPI_INT, writeTask, TAG_N, MPI_COMM_WORLD, &status); } /* fill file header */ for(n = 0; n < 6; n++) { header.npart[n] = ntot_type[n]; header.npartTotal[n] = (unsigned int) ntot_type_all[n]; header.npartTotalHighWord[n] = (unsigned int) (ntot_type_all[n] >> 32); } for(n = 0; n < 6; n++) header.mass[n] = All.MassTable[n]; header.time = All.Time; if(All.ComovingIntegrationOn) header.redshift = 1.0 / All.Time - 1; else header.redshift = 0; header.flag_sfr = 0; header.flag_feedback = 0; header.flag_cooling = 0; header.flag_stellarage = 0; header.flag_metals = 0; #ifdef COOLING header.flag_cooling = 1; #endif #ifdef SFR header.flag_sfr = 1; header.flag_feedback = 1; #ifdef STELLARAGE header.flag_stellarage = 1; #endif #ifdef METALS header.flag_metals = 1; #endif #endif header.num_files = All.NumFilesPerSnapshot; header.BoxSize = All.BoxSize; header.Omega0 = All.Omega0; header.OmegaLambda = All.OmegaLambda; header.HubbleParam = All.HubbleParam; /* open file and write header */ if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 sprintf(buf, "%s.hdf5", fname); hdf5_file = H5Fcreate(buf, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hdf5_headergrp = H5Gcreate(hdf5_file, "/Header", 0); for(type = 0; type < 6; type++) { if(header.npart[type] > 0) { sprintf(buf, "/PartType%d", type); hdf5_grp[type] = H5Gcreate(hdf5_file, buf, 0); } } write_header_attributes_in_hdf5(hdf5_headergrp); #endif } else { if(!(fd = fopen(fname, "w"))) { printf("can't open file `%s' for writing snapshot.\n", fname); endrun(123); } if(All.SnapFormat == 2) { blksize = sizeof(int) + 4 * sizeof(char); SKIP; my_fwrite("HEAD", sizeof(char), 4, fd); nextblock = sizeof(header) + 2 * sizeof(int); my_fwrite(&nextblock, sizeof(int), 1, fd); SKIP; } blksize = sizeof(header); SKIP; my_fwrite(&header, sizeof(header), 1, fd); SKIP; } } ntask = lastTask - writeTask + 1; for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++) { if(blockpresent(blocknr)) { bytes_per_blockelement = get_bytes_per_blockelement(blocknr); blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement; npart = get_particles_in_block(blocknr, &typelist[0]); if(npart > 0) { if(ThisTask == writeTask) { if(All.SnapFormat == 1 || All.SnapFormat == 2) { if(All.SnapFormat == 2) { blksize = sizeof(int) + 4 * sizeof(char); SKIP; my_fwrite(Tab_IO_Labels[blocknr], sizeof(char), 4, fd); nextblock = npart * bytes_per_blockelement + 2 * sizeof(int); my_fwrite(&nextblock, sizeof(int), 1, fd); SKIP; } blksize = npart * bytes_per_blockelement; SKIP; } } for(type = 0; type < 6; type++) { if(typelist[type]) { #ifdef HAVE_HDF5 if(ThisTask == writeTask && All.SnapFormat == 3 && header.npart[type] > 0) { switch (get_datatype_in_block(blocknr)) { case 0: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT); break; case 1: hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT); break; case 2: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64); break; } dims[0] = header.npart[type]; dims[1] = get_values_per_blockelement(blocknr); if(dims[1] == 1) rank = 1; else rank = 2; get_dataset_name(blocknr, buf); hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL); hdf5_dataset = H5Dcreate(hdf5_grp[type], buf, hdf5_datatype, hdf5_dataspace_in_file, H5P_DEFAULT); pcsum = 0; } #endif for(task = writeTask, offset = 0; task <= lastTask; task++) { if(task == ThisTask) { n_for_this_task = n_type[type]; for(p = writeTask; p <= lastTask; p++) if(p != ThisTask) MPI_Send(&n_for_this_task, 1, MPI_INT, p, TAG_NFORTHISTASK, MPI_COMM_WORLD); } else MPI_Recv(&n_for_this_task, 1, MPI_INT, task, TAG_NFORTHISTASK, MPI_COMM_WORLD, &status); while(n_for_this_task > 0) { pc = n_for_this_task; if(pc > blockmaxlen) pc = blockmaxlen; if(ThisTask == task) fill_write_buffer(blocknr, &offset, pc, type); if(ThisTask == writeTask && task != writeTask) MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA, MPI_COMM_WORLD, &status); if(ThisTask != writeTask && task == ThisTask) MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, writeTask, TAG_PDATA, MPI_COMM_WORLD); if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 start[0] = pcsum; start[1] = 0; count[0] = pc; count[1] = get_values_per_blockelement(blocknr); pcsum += pc; H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET, start, NULL, count, NULL); dims[0] = pc; dims[1] = get_values_per_blockelement(blocknr); hdf5_dataspace_memory = H5Screate_simple(rank, dims, NULL); hdf5_status = H5Dwrite(hdf5_dataset, hdf5_datatype, hdf5_dataspace_memory, hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer); H5Sclose(hdf5_dataspace_memory); #endif } else my_fwrite(CommBuffer, bytes_per_blockelement, pc, fd); } n_for_this_task -= pc; } } #ifdef HAVE_HDF5 if(ThisTask == writeTask && All.SnapFormat == 3 && header.npart[type] > 0) { if(All.SnapFormat == 3) { H5Dclose(hdf5_dataset); H5Sclose(hdf5_dataspace_in_file); H5Tclose(hdf5_datatype); } } #endif } } if(ThisTask == writeTask) { if(All.SnapFormat == 1 || All.SnapFormat == 2) SKIP; } } } } if(ThisTask == writeTask) { if(All.SnapFormat == 3) { #ifdef HAVE_HDF5 for(type = 5; type >= 0; type--) if(header.npart[type] > 0) H5Gclose(hdf5_grp[type]); H5Gclose(hdf5_headergrp); H5Fclose(hdf5_file); #endif } else fclose(fd); } }