5 template <
typename dtype>
7 printf(
"CTF ERROR: Format of tensor unsupported for sparse I/O\n");
44 template <
typename dtype>
48 if (!with_vals) mulid = *pmulid;
50 int64_t * ind = (int64_t *)malloc(order*
sizeof(int64_t));
51 for (i=0; i<nvals; i++) {
55 sscanf(lvals[i]+ptr,
"%ld%n", ind+0, &aptr);
57 for (
int j=1; j<order; j++){
58 sscanf(lvals[i]+ptr,
" %ld%n", ind+j, &aptr);
62 sscanf(lvals[i]+ptr, get_fmt<dtype>(), &v);
65 for (
int j=0; j<order; j++){
66 pairs[i].
k += ind[j]*lda;
90 template <
typename dtype>
94 int64_t * ind = (int64_t *)malloc(order*
sizeof(int64_t));
96 for (i=0; i<nvals; i++){
97 int64_t
key = pairs[i].
k;
98 for (
int j=0; j<order; j++){
99 ind[j] = key % lens[j];
104 astr_len += snprintf(NULL, 0,
"%ld", ind[0]);
105 for (
int j=1; j<order; j++){
106 astr_len += snprintf(NULL, 0,
" %ld", ind[j]);
109 astr_len += snprintf(NULL, 0, get_fmt<dtype>(), pairs[i].d);
110 astr_len += snprintf(NULL, 0,
"\n");
116 for (i=0; i<nvals; i++){
117 int64_t
key = pairs[i].
k;
118 for (
int j=0; j<order; j++){
119 ind[j] = key % lens[j];
123 str_ptr += sprintf(datastr+str_ptr,
"%ld", ind[0]);
124 for (
int j=1; j<order; j++){
125 str_ptr += sprintf(datastr+str_ptr,
" %ld", ind[j]);
128 str_ptr += sprintf(datastr+str_ptr, get_fmt<dtype>(), pairs[i].d);
129 str_ptr += sprintf(datastr+str_ptr,
"\n");
141 template <
typename dtype>
145 MPI_Offset localsize;
146 MPI_Offset start,end;
153 MPI_File_open(MPI_COMM_WORLD,fpath, MPI_MODE_RDONLY, MPI_INFO_NULL, &fh);
156 MPI_File_get_size(fh, &filesize);
158 localsize = filesize/dw->
np;
159 start = dw->
rank * localsize;
160 end = start + localsize;
163 if (dw->
rank == dw->
np-1) end = filesize;
164 localsize = end - start;
166 chunk = (
char*)malloc( (localsize + 1)*
sizeof(char));
167 MPI_File_read_at_all(fh, start, chunk, localsize, MPI_CHAR, &status);
168 chunk[localsize] =
'\0';
170 int64_t locstart=0, locend=localsize;
172 while(chunk[locstart] !=
'\n') locstart++;
175 if (dw->
rank != dw->
np-1) {
177 while(chunk[locend] !=
'\n') locend++;
180 localsize = locend-locstart;
183 memcpy(data, &(chunk[locstart]), localsize);
184 data[localsize] =
'\0';
188 for ( i=0; i<localsize; i++){
189 if (data[i] ==
'\n') ned++;
193 (*datastr) = (
char **)
CTF_int::alloc(std::max(ned,(int64_t)1)*
sizeof(
char *));
194 (*datastr)[0] = strtok(data,
"\n");
196 for ( i=1; i < ned; i++)
197 (*datastr)[i] = strtok(NULL,
"\n");
198 if ((*datastr)[0] == NULL)
213 template <
typename dtype>
220 MPI_File_open(MPI_COMM_WORLD, fpath, MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &fh);
222 MPI_File_open(MPI_COMM_WORLD, fpath, MPI_MODE_WRONLY | MPI_MODE_CREATE, MPI_INFO_NULL, &fh);
225 MPI_Scan(&str_len, &ioffset, 1, MPI_INT64_T, MPI_SUM, dw->
comm);
227 offset = ioffset - str_len;
229 MPI_File_write_at_all(fh, offset, datastr, str_len, MPI_CHAR, &status);
char * serialize_sparse_tensor_data(int order, int *lens, int64_t nvals, CTF::Pair< dtype > *pairs, bool with_vals, int64_t &str_len)
serialize sparse tensor data to create string
const char * get_fmt< float >()
dtype d
tensor value associated with index
void * alloc(int64_t len)
alloc abstraction
an instance of the CTF library (world) on a MPI communicator
int64_t k
key, global index [i1,i2,...] specified as i1+len[0]*i2+...
index-value pair used for tensor data input
const char * get_fmt< int >()
int rank
rank of local processor
const char * get_fmt< double >()
void write_data_mpiio(CTF::World const *dw, char const *fpath, char *datastr, int64_t str_len)
write sparse tensor data to file using MPI-I/O, from string with one entry per line (different entrie...
int64_t read_data_mpiio(CTF::World const *dw, char const *fpath, char ***datastr)
read sparse tensor data from file using MPI-I/O, creating string with one entry per line (different e...
int cdealloc(void *ptr)
free abstraction
const char * get_fmt()
return format string for templated type
int np
number of processors
MPI_Comm comm
set of processors making up this world
const char * get_fmt< int64_t >()
void parse_sparse_tensor_data(char **lvals, int order, dtype const *pmulid, int *lens, int64_t nvals, CTF::Pair< dtype > *pairs, bool with_vals)
parse string containing sparse tensor into data