28static int32_t defaultStringLength = SDDS_MPI_STRING_COLUMN_LEN;
29static int32_t number_of_string_truncated = 0;
30static int32_t defaultTitleBufferSize = 2400000;
31static int32_t defaultReadBufferSize = 4000000;
32static int32_t defaultWriteBufferSize = 0;
35static FILE *fpdeb = NULL;
51 return defaultReadBufferSize;
52 previous = defaultReadBufferSize;
53 defaultReadBufferSize = newSize;
70 return defaultWriteBufferSize;
71 previous = defaultWriteBufferSize;
72 defaultWriteBufferSize = newSize;
89 return defaultTitleBufferSize;
90 previous = defaultTitleBufferSize;
91 defaultTitleBufferSize = newSize;
104 return number_of_string_truncated;
113 number_of_string_truncated++;
129 return defaultStringLength;
130 previous = defaultStringLength;
131 defaultStringLength = newValue;
146 logDebug(
"SDDS_MPI_WriteBinaryPage", SDDS_dataset);
163 static char *dummy_string =
"";
167 logDebug(
"SDDS_MPI_WriteBinaryString", SDDS_dataset);
171 string = dummy_string;
173 length = strlen(
string);
193 static char *dummy_string =
"";
197 logDebug(
"SDDS_MPI_WriteNonNativeBinaryString", SDDS_dataset);
201 string = dummy_string;
203 length = strlen(
string);
231 logDebug(
"SDDS_MPI_WriteBinaryParameters", SDDS_dataset);
237 layout = &SDDS_dataset->layout;
238 for (i = 0; i < layout->n_parameters; i++) {
239 if (layout->parameter_definition[i].fixed_value)
241 if (layout->parameter_definition[i].type ==
SDDS_STRING) {
271 logDebug(
"SDDS_MPI_WriteNonNativeBinaryParameters", SDDS_dataset);
277 layout = &SDDS_dataset->layout;
279 for (i = 0; i < layout->n_parameters; i++) {
280 if (layout->parameter_definition[i].fixed_value)
282 if (layout->parameter_definition[i].type ==
SDDS_STRING) {
311 int32_t i, j, zero = 0, writeSize = 0;
317 layout = &SDDS_dataset->layout;
318 for (i = 0; i < layout->n_arrays; i++) {
319 if (!SDDS_dataset->array[i].dimension) {
320 for (j = 0; j < layout->array_definition[i].dimensions; j++) {
322 SDDS_SetError(
"Unable to write null array--failure writing dimensions (SDDS_MPI_WriteBinaryArrays)");
328 writeSize =
sizeof(*(SDDS_dataset->array)[i].dimension) * layout->array_definition[i].dimensions;
330 SDDS_SetError(
"Unable to write arrays--failure writing dimensions (SDDS_MPI_WriteBinaryArrays)");
333 if (layout->array_definition[i].type ==
SDDS_STRING) {
334 for (j = 0; j < SDDS_dataset->array[i].elements; j++) {
336 SDDS_SetError(
"Unable to write arrays--failure writing string (SDDS_WriteBinaryArrays)");
341 writeSize =
SDDS_type_size[layout->array_definition[i].type - 1] * SDDS_dataset->array[i].elements;
343 SDDS_SetError(
"Unable to write arrays--failure writing values (SDDS_MPI_WriteBinaryArrays)");
365 int32_t i, j, zero = 0, writeSize = 0;
373 layout = &SDDS_dataset->layout;
374 for (i = 0; i < layout->n_arrays; i++) {
375 if (!SDDS_dataset->array[i].dimension) {
376 for (j = 0; j < layout->array_definition[i].dimensions; j++) {
378 SDDS_SetError(
"Unable to write null array--failure writing dimensions (SDDS_MPI_WriteBinaryArrays)");
385 writeSize =
sizeof(*(SDDS_dataset->array)[i].dimension) * layout->array_definition[i].dimensions;
387 SDDS_SetError(
"Unable to write arrays--failure writing dimensions (SDDS_MPI_WriteBinaryArrays)");
391 if (layout->array_definition[i].type ==
SDDS_STRING) {
392 for (j = 0; j < SDDS_dataset->array[i].elements; j++) {
394 SDDS_SetError(
"Unable to write arrays--failure writing string (SDDS_WriteBinaryArrays)");
400 writeSize =
SDDS_type_size[layout->array_definition[i].type - 1] * SDDS_dataset->array[i].elements;
402 SDDS_SetError(
"Unable to write arrays--failure writing values (SDDS_MPI_WriteBinaryArrays)");
412static long SDDS_MPI_write_kludge_usleep = 0;
423 SDDS_MPI_write_kludge_usleep = value;
426static short SDDS_MPI_force_file_sync = 0;
438 SDDS_MPI_force_file_sync = value;
462 logDebug(
"SDDS_MPI_WriteBinaryRow", SDDS_dataset);
468 layout = &SDDS_dataset->layout;
470 sprintf(format,
"%%-%ds", defaultStringLength);
471 if (!(buff = malloc(
sizeof(*buff) * (defaultStringLength + 1)))) {
472 SDDS_SetError(
"Can not allocate memory in SDDS_MPI_WriteBinaryRow!");
475 buff[defaultStringLength] = 0;
476 for (i = 0; i < layout->n_columns; i++) {
477 type = layout->column_definition[i].type;
481 if (strlen(*((
char **)SDDS_dataset->data[i] + row)) <= defaultStringLength)
482 sprintf(buff, format, *((
char **)SDDS_dataset->data[i] + row));
484 strncpy(buff, *((
char **)SDDS_dataset->data[i] + row), defaultStringLength);
485 number_of_string_truncated++;
500 if (SDDS_MPI_write_kludge_usleep)
502 if (SDDS_MPI_force_file_sync)
503 MPI_File_sync(SDDS_dataset->MPI_dataset->MPI_file);
527 logDebug(
"SDDS_MPI_WriteNonNativeBinaryRow", SDDS_dataset);
533 layout = &SDDS_dataset->layout;
535 sprintf(format,
"%%-%ds", defaultStringLength);
536 if (!(buff = malloc(
sizeof(*buff) * (defaultStringLength + 1)))) {
537 SDDS_SetError(
"Can not allocate memory in SDDS_MPI_WriteNonNativeBinaryRow!");
540 buff[defaultStringLength] = 0;
541 for (i = 0; i < layout->n_columns; i++) {
542 type = layout->column_definition[i].type;
546 if (strlen(*((
char **)SDDS_dataset->data[i] + row)) <= defaultStringLength)
547 sprintf(buff, format, *((
char **)SDDS_dataset->data[i] + row));
549 strncpy(buff, *((
char **)SDDS_dataset->data[i] + row), defaultStringLength);
550 number_of_string_truncated++;
565 if (SDDS_MPI_write_kludge_usleep)
567 if (SDDS_MPI_force_file_sync)
568 MPI_File_sync(SDDS_dataset->MPI_dataset->MPI_file);
584 MPI_Offset column_offset = 0;
587 layout = &SDDS_dataset->layout;
588 for (i = 0; i < layout->n_columns; i++) {
589 if (layout->column_definition[i].type ==
SDDS_STRING)
592 column_offset +=
sizeof(int32_t) + defaultStringLength *
sizeof(
char);
594 column_offset +=
SDDS_type_size[layout->column_definition[i].type - 1];
596 return column_offset;
613 MPI_DATASET *MPI_dataset;
617 logDebug(
"SDDS_MPI_BufferedWrite", SDDS_dataset);
619 MPI_dataset = SDDS_dataset->MPI_dataset;
620 fBuffer = &(SDDS_dataset->fBuffer);
622 if (!fBuffer->bufferSize) {
623 if ((mpi_code = MPI_File_write(MPI_dataset->MPI_file, target, targetSize, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
624 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
629 if ((fBuffer->bytesLeft -= targetSize) >= 0) {
630 memcpy((
char *)fBuffer->data, (
char *)target, targetSize);
631 fBuffer->data += targetSize;
633 fprintf(stderr,
"SDDS_MPI_BufferedWrite of %" PRId64
" bytes done in-memory, %" PRId64
" bytes left\n", targetSize, fBuffer->bytesLeft);
642 lastLeft = (fBuffer->bytesLeft += targetSize);
644 memcpy((
char *)fBuffer->data, (
char *)target, (
size_t)fBuffer->bytesLeft);
645 if ((mpi_code = MPI_File_write(MPI_dataset->MPI_file, fBuffer->buffer, (
int)(fBuffer->bufferSize), MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
646 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
654 fBuffer->data = fBuffer->buffer;
655 if ((targetSize -= lastLeft) > (fBuffer->bytesLeft = fBuffer->bufferSize)) {
656 if ((mpi_code = MPI_File_write_at(MPI_dataset->MPI_file, (MPI_Offset)(MPI_dataset->file_offset), (
char *)target + lastLeft, targetSize, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
657 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
665 memcpy((
char *)fBuffer->data, (
char *)target + lastLeft, targetSize);
666 fBuffer->data += targetSize;
667 fBuffer->bytesLeft -= targetSize;
685 MPI_DATASET *MPI_dataset;
689 logDebug(
"SDDS_MPI_BufferedWriteAll", SDDS_dataset);
691 MPI_dataset = SDDS_dataset->MPI_dataset;
692 fBuffer = &(SDDS_dataset->fBuffer);
694 if (!fBuffer->bufferSize) {
695 if ((mpi_code = MPI_File_write_all(MPI_dataset->MPI_file, target, targetSize, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
696 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
701 if ((fBuffer->bytesLeft -= targetSize) >= 0) {
702 memcpy((
char *)fBuffer->data, (
char *)target, targetSize);
703 fBuffer->data += targetSize;
705 fprintf(stderr,
"SDDS_MPI_BufferedWrite of %" PRId64
" bytes done in-memory, %" PRId64
" bytes left\n", targetSize, fBuffer->bytesLeft);
714 lastLeft = (fBuffer->bytesLeft += targetSize);
716 memcpy((
char *)fBuffer->data, (
char *)target, (
size_t)fBuffer->bytesLeft);
717 if ((mpi_code = MPI_File_write_all(MPI_dataset->MPI_file, fBuffer->buffer, (
int)(fBuffer->bufferSize), MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
718 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
726 fBuffer->data = fBuffer->buffer;
727 if ((targetSize -= lastLeft) > (fBuffer->bytesLeft = fBuffer->bufferSize)) {
728 if ((mpi_code = MPI_File_write_all(MPI_dataset->MPI_file, (
char *)target + lastLeft, targetSize, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
729 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_WriteBufferedWrite(MPI_File_write_at failed)", mpi_code, 0);
737 memcpy((
char *)fBuffer->data, (
char *)target + lastLeft, targetSize);
738 fBuffer->data += targetSize;
739 fBuffer->bytesLeft -= targetSize;
757 MPI_DATASET *MPI_dataset;
762 logDebug(
"SDDS_MPI_FlushBuffer", SDDS_dataset);
765 MPI_dataset = SDDS_dataset->MPI_dataset;
766 fBuffer = &(SDDS_dataset->fBuffer);
768 if (!fBuffer->bufferSize)
771 if ((writeBytes = fBuffer->bufferSize - fBuffer->bytesLeft)) {
772 if (writeBytes < 0) {
773 SDDS_SetError(
"Unable to flush buffer: negative byte count (SDDS_FlushBuffer).");
777 fprintf(stderr,
"Writing %" PRId64
" bytes to disk\n", writeBytes);
779 if ((mpi_code = MPI_File_write(MPI_dataset->MPI_file, fBuffer->buffer, writeBytes, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
780 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_FlushBuffer(MPI_File_write_at failed)", mpi_code, 0);
783 fBuffer->bytesLeft = fBuffer->bufferSize;
784 fBuffer->data = fBuffer->buffer;
802 for (i = start_row; i < end_row; i++) {
803 if (i > SDDS_dataset->n_rows - 1)
805 if (SDDS_dataset->row_flag[i])
822 MPI_Reduce(&(SDDS_dataset->n_rows), &total_rows, 1, MPI_INT64_T, MPI_SUM, 0, SDDS_dataset->MPI_dataset->comm);
840 int64_t row, prev_rows, i, total_rows, fixed_rows, rows;
841 int mpi_code, type = 0;
842 MPI_Offset column_offset, rowcount_offset, offset;
843 int64_t *n_rows = NULL;
845 int32_t min32 = INT32_MIN;
847 MPI_DATASET *MPI_dataset = NULL;
851 logDebug(
"SDDS_MPI_WriteNonNativeBinaryPage", SDDS_dataset);
854 MPI_dataset = SDDS_dataset->MPI_dataset;
863 fBuffer = &SDDS_dataset->fBuffer;
864 if (SDDS_dataset->layout.data_mode.column_major)
866 rows = SDDS_dataset->n_rows;
869 if (!fBuffer->buffer) {
870 fBuffer->bufferSize = defaultWriteBufferSize;
871 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
872 SDDS_SetError(
"Unable to do buffered read--allocation failure (SDDS_WriteNonNativeBinaryPage)");
875 fBuffer->bytesLeft = fBuffer->bufferSize;
876 fBuffer->data[0] = 0;
879 if (MPI_dataset->n_page >= 1)
880 MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL);
883 column_offset = MPI_dataset->column_offset;
884 if (!(n_rows = calloc(
sizeof(*n_rows), MPI_dataset->n_processors))) {
888 MPI_Allgather(&rows, 1, MPI_INT64_T, n_rows, 1, MPI_INT64_T, MPI_dataset->comm);
890 for (i = 0; i < MPI_dataset->myid; i++)
891 prev_rows += n_rows[i];
893 for (i = 0; i < MPI_dataset->n_processors; i++)
894 total_rows += n_rows[i];
895 if (MPI_dataset->myid == 0) {
896 fixed_rows = total_rows;
897 if (!fBuffer->buffer) {
898 fBuffer->bufferSize = defaultWriteBufferSize;
899 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
900 SDDS_SetError(
"Unable to do buffered read--allocation failure (SDDS_WriteNonNativeBinaryPage)");
903 fBuffer->bytesLeft = fBuffer->bufferSize;
904 fBuffer->data[0] = 0;
906 if (fixed_rows > INT32_MAX) {
914 int32_t fixed_rows32;
915 fixed_rows32 = (int32_t)fixed_rows;
927 if (SDDS_dataset->layout.data_mode.column_major) {
929 offset = rowcount_offset;
930 for (i = 0; i < SDDS_dataset->layout.n_columns; i++) {
931 type = SDDS_dataset->layout.column_definition[i].type;
932 MPI_dataset->file_offset = offset + (MPI_Offset)prev_rows *
SDDS_type_size[type - 1];
934 SDDS_SetError(
"Can not write string column to SDDS3 (SDDS_MPI_WriteNonNativeBinaryPage");
937 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
942 if ((mpi_code = MPI_File_write(MPI_dataset->MPI_file, SDDS_dataset->data[i], rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
943 SDDS_SetError(
"Unable to write binary columns (SDDS_MPI_WriteNonNativeBinaryPage");
949 MPI_dataset->file_offset = offset;
952 MPI_dataset->file_offset = rowcount_offset + (MPI_Offset)prev_rows * column_offset;
954 MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL);
955 if (!MPI_dataset->collective_io) {
957 for (i = 0; i < SDDS_dataset->n_rows; i++) {
963 SDDS_dataset->n_rows = row;
969 row = SDDS_dataset->n_rows;
971 MPI_Allreduce(&row, &total_rows, 1, MPI_INT64_T, MPI_SUM, MPI_dataset->comm);
972 MPI_dataset->file_offset = rowcount_offset + total_rows * column_offset;
978 SDDS_dataset->last_row_written = SDDS_dataset->n_rows - 1;
979 SDDS_dataset->n_rows_written = rows;
980 SDDS_dataset->writing_page = 1;
981 MPI_dataset->n_page++;
998 int64_t row, prev_rows, i, total_rows, fixed_rows, rows;
999 int mpi_code, type = 0;
1000 MPI_Offset column_offset, rowcount_offset, offset;
1001 int64_t *n_rows = NULL;
1002 int32_t min32 = INT32_MIN;
1005 MPI_DATASET *MPI_dataset = NULL;
1007 char *outputEndianess = NULL;
1010 logDebug(
"SDDS_MPI_WriteContinuousBinaryPage", SDDS_dataset);
1015 if ((outputEndianess = getenv(
"SDDS_OUTPUT_ENDIANESS"))) {
1020 MPI_dataset = SDDS_dataset->MPI_dataset;
1025 fBuffer = &SDDS_dataset->fBuffer;
1026 if (SDDS_dataset->layout.data_mode.column_major)
1028 rows = SDDS_dataset->n_rows;
1031 if (!fBuffer->buffer) {
1032 fBuffer->bufferSize = defaultWriteBufferSize;
1033 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
1034 SDDS_SetError(
"Unable to do buffered read--allocation failure (SDDS_WriteContinuousBinaryPage)");
1037 fBuffer->bytesLeft = fBuffer->bufferSize;
1038 fBuffer->data[0] = 0;
1041 if (MPI_dataset->n_page >= 1)
1042 MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL);
1045 column_offset = MPI_dataset->column_offset;
1046 if (!(n_rows = calloc(
sizeof(*n_rows), MPI_dataset->n_processors))) {
1050 MPI_Allgather(&rows, 1, MPI_INT64_T, n_rows, 1, MPI_INT64_T, MPI_dataset->comm);
1052 for (i = 0; i < MPI_dataset->myid; i++)
1053 prev_rows += n_rows[i];
1055 for (i = 0; i < MPI_dataset->n_processors; i++)
1056 total_rows += n_rows[i];
1057 if (MPI_dataset->myid == 0) {
1058 fixed_rows = total_rows;
1059 if (!fBuffer->buffer) {
1060 fBuffer->bufferSize = defaultWriteBufferSize;
1061 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
1062 SDDS_SetError(
"Unable to do buffered read--allocation failure (SDDS_WriteContinuousBinaryPage)");
1065 fBuffer->bytesLeft = fBuffer->bufferSize;
1066 fBuffer->data[0] = 0;
1068 if (fixed_rows > INT32_MAX) {
1074 int32_t fixed_rows32;
1075 fixed_rows32 = (int32_t)fixed_rows;
1086 if (SDDS_dataset->layout.data_mode.column_major) {
1088 offset = rowcount_offset;
1089 for (i = 0; i < SDDS_dataset->layout.n_columns; i++) {
1090 type = SDDS_dataset->layout.column_definition[i].type;
1091 MPI_dataset->file_offset = offset + (MPI_Offset)prev_rows *
SDDS_type_size[type - 1];
1093 SDDS_SetError(
"Can not write string column to SDDS3 (SDDS_MPI_WriteContinuousBinaryPage");
1096 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
1101 if ((mpi_code = MPI_File_write(MPI_dataset->MPI_file, SDDS_dataset->data[i], rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
1102 SDDS_SetError(
"Unable to write binary columns (SDDS_MPI_WriteContinuousBinaryPage");
1108 MPI_dataset->file_offset = offset;
1111 MPI_dataset->file_offset = rowcount_offset + (MPI_Offset)prev_rows * column_offset;
1113 MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL);
1114 if (!MPI_dataset->collective_io) {
1116 for (i = 0; i < SDDS_dataset->n_rows; i++) {
1122 SDDS_dataset->n_rows = row;
1128 row = SDDS_dataset->n_rows;
1130 MPI_Allreduce(&row, &total_rows, 1, MPI_INT64_T, MPI_SUM, MPI_dataset->comm);
1131 MPI_dataset->file_offset = rowcount_offset + total_rows * column_offset;
1136 SDDS_dataset->last_row_written = SDDS_dataset->n_rows - 1;
1137 SDDS_dataset->n_rows_written = rows;
1138 SDDS_dataset->writing_page = 1;
1139 MPI_dataset->n_page++;
1162 int32_t bytesRead, count;
1164 MPI_DATASET *MPI_dataset = SDDS_dataset->MPI_dataset;
1166 if (!fBuffer || !fBuffer->bufferSize) {
1169 mpi_code = MPI_File_seek(MPI_dataset->MPI_file, targetSize, MPI_SEEK_CUR);
1171 mpi_code = MPI_File_read(MPI_dataset->MPI_file, target, targetSize, MPI_BYTE, &status);
1172 MPI_Get_count(&status, MPI_BYTE, &bytesRead);
1174 MPI_dataset->end_of_file = 1;
1177 if (bytesRead < targetSize)
1180 if (mpi_code != MPI_SUCCESS) {
1186 if ((fBuffer->bytesLeft -= targetSize) >= 0) {
1189 memcpy((
char *)target, (
char *)fBuffer->data, targetSize);
1191 fBuffer->data += targetSize;
1195 int64_t bytesNeeded, offset;
1196 fBuffer->bytesLeft += targetSize;
1198 if ((offset = fBuffer->bytesLeft)) {
1201 memcpy((
char *)target, (
char *)fBuffer->data, offset);
1203 bytesNeeded = targetSize - offset;
1204 fBuffer->bytesLeft = 0;
1206 bytesNeeded = targetSize;
1208 fBuffer->data = fBuffer->buffer;
1209 if (fBuffer->bufferSize < bytesNeeded) {
1212 mpi_code = MPI_File_seek(MPI_dataset->MPI_file, targetSize, MPI_SEEK_CUR);
1214 mpi_code = MPI_File_read(MPI_dataset->MPI_file, (
char *)target + offset, bytesNeeded, MPI_BYTE, &status);
1215 MPI_Get_count(&status, MPI_BYTE, &bytesRead);
1217 MPI_dataset->end_of_file = 1;
1220 if (bytesRead < bytesNeeded)
1223 if (mpi_code != MPI_SUCCESS) {
1224 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_ReadBufferedRead(MPI_File_read failed)", mpi_code, 0);
1230 mpi_code = MPI_File_read(MPI_dataset->MPI_file, fBuffer->data, (
int)(fBuffer->bufferSize), MPI_BYTE, &status);
1231 MPI_Get_count(&status, MPI_BYTE, &count);
1232 fBuffer->bytesLeft = count;
1233 if (!(fBuffer->bytesLeft))
1234 MPI_dataset->end_of_file = 1;
1235 if (fBuffer->bytesLeft < bytesNeeded)
1238 memcpy((
char *)target + offset, (
char *)fBuffer->data, bytesNeeded);
1239 fBuffer->data += bytesNeeded;
1240 fBuffer->bytesLeft -= bytesNeeded;
1262 int32_t mpi_code, bytesRead, count;
1264 MPI_DATASET *MPI_dataset = SDDS_dataset->MPI_dataset;
1266 if (!fBuffer || !fBuffer->bufferSize) {
1269 mpi_code = MPI_File_seek(MPI_dataset->MPI_file, targetSize, MPI_SEEK_CUR);
1271 mpi_code = MPI_File_read_all(MPI_dataset->MPI_file, target, targetSize, MPI_BYTE, &status);
1272 MPI_Get_count(&status, MPI_BYTE, &bytesRead);
1274 MPI_dataset->end_of_file = 1;
1277 if (bytesRead < targetSize)
1280 if (mpi_code != MPI_SUCCESS) {
1286 if ((fBuffer->bytesLeft -= targetSize) >= 0) {
1289 memcpy((
char *)target, (
char *)fBuffer->data, targetSize);
1291 fBuffer->data += targetSize;
1295 int64_t bytesNeeded, offset;
1296 fBuffer->bytesLeft += targetSize;
1298 if ((offset = fBuffer->bytesLeft)) {
1301 memcpy((
char *)target, (
char *)fBuffer->data, offset);
1303 bytesNeeded = targetSize - offset;
1304 fBuffer->bytesLeft = 0;
1306 bytesNeeded = targetSize;
1308 fBuffer->data = fBuffer->buffer;
1309 if (fBuffer->bufferSize < bytesNeeded) {
1312 mpi_code = MPI_File_seek(MPI_dataset->MPI_file, targetSize, MPI_SEEK_CUR);
1314 mpi_code = MPI_File_read_all(MPI_dataset->MPI_file, (
char *)target + offset, bytesNeeded, MPI_BYTE, &status);
1315 MPI_Get_count(&status, MPI_BYTE, &bytesRead);
1317 MPI_dataset->end_of_file = 1;
1320 if (bytesRead < bytesNeeded)
1323 if (mpi_code != MPI_SUCCESS) {
1324 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_ReadBufferedRead(MPI_File_read failed)", mpi_code, 0);
1330 mpi_code = MPI_File_read_all(MPI_dataset->MPI_file, fBuffer->data, (
int)(fBuffer->bufferSize), MPI_BYTE, &status);
1331 MPI_Get_count(&status, MPI_BYTE, &count);
1332 fBuffer->bytesLeft = count;
1333 if (!(fBuffer->bytesLeft))
1334 MPI_dataset->end_of_file = 1;
1335 if (fBuffer->bytesLeft < bytesNeeded)
1338 memcpy((
char *)target + offset, (
char *)fBuffer->data, bytesNeeded);
1339 fBuffer->data += bytesNeeded;
1340 fBuffer->bytesLeft -= bytesNeeded;
1366 if (!(
string =
SDDS_Malloc(
sizeof(*
string) * (length + 1))))
1368 if (length && !
SDDS_MPI_BufferedRead(skip ? NULL :
string,
sizeof(*
string) * length, SDDS_dataset, fBuffer))
1399 if (!(
string =
SDDS_Malloc(
sizeof(*
string) * (length + 1))))
1401 if (length && !
SDDS_MPI_BufferedRead(skip ? NULL :
string,
sizeof(*
string) * length, SDDS_dataset, fBuffer))
1424 static char buffer[SDDS_MAXLINE];
1428 layout = &SDDS_dataset->layout;
1429 if (!layout->n_parameters)
1431 for (i = 0; i < layout->n_parameters; i++) {
1432 if (layout->parameter_definition[i].definition_mode & SDDS_WRITEONLY_DEFINITION)
1434 if (layout->parameter_definition[i].fixed_value) {
1435 strcpy(buffer, layout->parameter_definition[i].fixed_value);
1436 if (!
SDDS_ScanData(buffer, layout->parameter_definition[i].type, 0, SDDS_dataset->parameter[i], 0, 1)) {
1437 SDDS_SetError(
"Unable to read page--parameter scanning error (SDDS_MPI_ReadBinaryParameters)");
1440 }
else if (layout->parameter_definition[i].type ==
SDDS_STRING) {
1441 if (*(
char **)SDDS_dataset->parameter[i])
1442 free(*(
char **)SDDS_dataset->parameter[i]);
1444 SDDS_SetError(
"Unable to read parameters--failure reading string (SDDS_MPI_ReadBinaryParameters)");
1449 SDDS_SetError(
"Unable to read parameters--failure reading value (SDDS_MPI_ReadBinaryParameters)");
1479 layout = &SDDS_dataset->layout;
1480 fBuffer = &SDDS_dataset->fBuffer;
1482 for (i = 0; i < layout->n_columns; i++) {
1483 if (layout->column_definition[i].definition_mode & SDDS_WRITEONLY_DEFINITION)
1485 if ((type = layout->column_definition[i].type) ==
SDDS_STRING) {
1487 if (((
char ***)SDDS_dataset->data)[i][row])
1488 free((((
char ***)SDDS_dataset->data)[i][row]));
1490 SDDS_SetError(
"Unable to read rows--failure reading string (SDDS_MPI_ReadBinaryRows)");
1495 SDDS_SetError(
"Unable to read rows--failure reading string (SDDS_MPI_ReadBinaryRows)");
1501 if (!
SDDS_MPI_BufferedRead(skip ? NULL : (
char *)SDDS_dataset->data[i] + row * size, size, SDDS_dataset, fBuffer)) {
1502 SDDS_SetError(
"Unable to read row--failure reading value (SDDS_MPI_ReadBinaryRow)");
1532 layout = &SDDS_dataset->layout;
1533 if (!layout->n_arrays)
1536 if (!SDDS_dataset->array) {
1537 SDDS_SetError(
"Unable to read array--pointer to structure storage area is NULL (SDDS_MPI_ReadBinaryArrays)");
1540 for (i = 0; i < layout->n_arrays; i++) {
1541 array = SDDS_dataset->array + i;
1543 SDDS_SetError(
"Unable to get array--array definition corrupted (SDDS_MPI_ReadBinaryArrays)");
1547 SDDS_SetError(
"Unable to read array--definition copy failed (SDDS_MPI_ReadBinaryArrays)");
1551 if (!(array->dimension =
SDDS_Realloc(array->dimension,
sizeof(*array->dimension) * array->definition->dimensions))) {
1552 SDDS_SetError(
"Unable to read array--allocation failure (SDDS_MPI_ReadBinaryArrays)");
1555 if (!
SDDS_MPI_BufferedRead(array->dimension,
sizeof(*array->dimension) * array->definition->dimensions, SDDS_dataset, fBuffer)) {
1556 SDDS_SetError(
"Unable to read arrays--failure reading dimensions (SDDS_MPI_ReadBinaryArrays)");
1559 array->elements = 1;
1560 for (j = 0; j < array->definition->dimensions; j++)
1561 array->elements *= array->dimension[j];
1564 array->data = array->pointer = NULL;
1565 if (array->elements == 0)
1567 if (array->elements < 0) {
1568 SDDS_SetError(
"Unable to read array--number of elements is negative (SDDS_MPI_ReadBinaryArrays)");
1572 SDDS_SetError(
"Unable to read array--allocation failure (SDDS_MPI_ReadBinaryArrays)");
1576 for (j = 0; j < array->elements; j++) {
1578 SDDS_SetError(
"Unable to read arrays--failure reading string (SDDS_MPI_ReadBinaryArrays)");
1584 SDDS_SetError(
"Unable to read arrays--failure reading values (SDDS_MPI_ReadBinaryArrays)");
1609 static char buffer[SDDS_MAXLINE];
1613 layout = &SDDS_dataset->layout;
1614 if (!layout->n_parameters)
1616 for (i = 0; i < layout->n_parameters; i++) {
1617 if (layout->parameter_definition[i].definition_mode & SDDS_WRITEONLY_DEFINITION)
1619 if (layout->parameter_definition[i].fixed_value) {
1620 strcpy(buffer, layout->parameter_definition[i].fixed_value);
1621 if (!
SDDS_ScanData(buffer, layout->parameter_definition[i].type, 0, SDDS_dataset->parameter[i], 0, 1)) {
1622 SDDS_SetError(
"Unable to read page--parameter scanning error (SDDS_MPI_ReadNonNativeBinaryParameters)");
1625 }
else if (layout->parameter_definition[i].type ==
SDDS_STRING) {
1626 if (*(
char **)SDDS_dataset->parameter[i])
1627 free(*(
char **)SDDS_dataset->parameter[i]);
1629 SDDS_SetError(
"Unable to read parameters--failure reading string (SDDS_MPI_ReadNonNativeBinaryParameters)");
1634 SDDS_SetError(
"Unable to read parameters--failure reading value (SDDS_MPI_ReadNonNativeBinaryParameters)");
1661 layout = &SDDS_dataset->layout;
1662 if (!layout->n_arrays)
1665 if (!SDDS_dataset->array) {
1666 SDDS_SetError(
"Unable to read array--pointer to structure storage area is NULL (SDDS_MPI_ReadNonNativeBinaryArrays)");
1669 for (i = 0; i < layout->n_arrays; i++) {
1670 array = SDDS_dataset->array + i;
1672 SDDS_SetError(
"Unable to get array--array definition corrupted (SDDS_MPI_ReadNonNativeBinaryArrays)");
1676 SDDS_SetError(
"Unable to read array--definition copy failed (SDDS_MPI_ReadNonNativeBinaryArrays)");
1680 if (!(array->dimension =
SDDS_Realloc(array->dimension,
sizeof(*array->dimension) * array->definition->dimensions))) {
1681 SDDS_SetError(
"Unable to read array--allocation failure (SDDS_MPI_ReadNonNativeBinaryArrays)");
1684 if (!
SDDS_MPI_BufferedRead(array->dimension,
sizeof(*array->dimension) * array->definition->dimensions, SDDS_dataset, fBuffer)) {
1685 SDDS_SetError(
"Unable to read arrays--failure reading dimensions (SDDS_MPI_ReadNonNativeBinaryArrays)");
1689 array->elements = 1;
1690 for (j = 0; j < array->definition->dimensions; j++)
1691 array->elements *= array->dimension[j];
1694 array->data = array->pointer = NULL;
1695 if (array->elements == 0)
1697 if (array->elements < 0) {
1698 SDDS_SetError(
"Unable to read array--number of elements is negative (SDDS_MPI_ReadNonNativeBinaryArrays)");
1702 SDDS_SetError(
"Unable to read array--allocation failure (SDDS_MPI_ReadNonNativeBinaryArrays)");
1706 for (j = 0; j < array->elements; j++) {
1708 SDDS_SetError(
"Unable to read arrays--failure reading string (SDDS_MPI_ReadNonNativeBinaryArrays)");
1714 SDDS_SetError(
"Unable to read arrays--failure reading values (SDDS_MPI_ReadNonNativeBinaryArrays)");
1743 layout = &SDDS_dataset->layout;
1744 fBuffer = &SDDS_dataset->fBuffer;
1746 for (i = 0; i < layout->n_columns; i++) {
1747 if (layout->column_definition[i].definition_mode & SDDS_WRITEONLY_DEFINITION)
1749 if ((type = layout->column_definition[i].type) ==
SDDS_STRING) {
1751 if (((
char ***)SDDS_dataset->data)[i][row])
1752 free((((
char ***)SDDS_dataset->data)[i][row]));
1754 SDDS_SetError(
"Unable to read rows--failure reading string (SDDS_MPI_ReadNonNativeBinaryRow)");
1759 SDDS_SetError(
"Unable to read rows--failure reading string (SDDS_MPI_ReadNonNativeBinaryRow)");
1765 if (!
SDDS_MPI_BufferedRead(skip ? NULL : (
char *)SDDS_dataset->data[i] + row * size, size, SDDS_dataset, fBuffer)) {
1766 SDDS_SetError(
"Unable to read row--failure reading value (SDDS_MPI_ReadNonNativeBinaryRow)");
1785 char *par_data = NULL;
1787 int64_t i, count = 0, *data_len = NULL;
1788 MPI_DATASET *MPI_dataset;
1790 char *
string = NULL;
1792 MPI_dataset = SDDS_dataset->MPI_dataset;
1793 layout = &(SDDS_dataset->layout);
1794 if (!layout->n_parameters && !layout->n_arrays) {
1796 MPI_Bcast(&(MPI_dataset->total_rows), 1, MPI_INT64_T, 0, MPI_dataset->comm);
1799 data_len = calloc(
sizeof(*data_len), 1 + layout->n_parameters);
1800 if (MPI_dataset->myid == 0) {
1801 data_len[0] =
sizeof(int64_t);
1802 count = data_len[0];
1803 for (i = 0; i < layout->n_parameters; i++) {
1804 type = layout->parameter_definition[i].type;
1806 data_len[i + 1] = strlen(*((
char **)SDDS_dataset->parameter[i])) *
sizeof(char);
1810 count += data_len[i + 1];
1812 par_data = (
char *)malloc(
sizeof(
char) * count);
1813 memcpy((
char *)par_data, &(MPI_dataset->total_rows), data_len[0]);
1814 count = data_len[0];
1815 for (i = 0; i < layout->n_parameters; i++) {
1816 if (layout->parameter_definition[i].type ==
SDDS_STRING)
1817 memcpy((
char *)par_data + count, *(
char **)SDDS_dataset->parameter[i], data_len[i + 1]);
1819 memcpy((
char *)par_data + count, (
char *)SDDS_dataset->parameter[i], data_len[i + 1]);
1820 count += data_len[i + 1];
1823 MPI_Bcast(data_len, 1 + layout->n_parameters, MPI_INT64_T, 0, MPI_dataset->comm);
1824 if (MPI_dataset->myid) {
1825 count = data_len[0];
1826 for (i = 0; i < layout->n_parameters; i++)
1827 count += data_len[i + 1];
1828 par_data = (
char *)malloc(
sizeof(
char) * count);
1831 MPI_Bcast(par_data, count, MPI_BYTE, 0, MPI_dataset->comm);
1833 SDDS_SetError(
"Unable to read page--couldn't start page (SDDS_MPI_BroadcastTitleData)");
1836 if (MPI_dataset->myid) {
1837 memcpy(&(MPI_dataset->total_rows), (
char *)par_data, data_len[0]);
1838 count = data_len[0];
1839 for (i = 0; i < layout->n_parameters; i++) {
1840 if (layout->parameter_definition[i].type ==
SDDS_STRING) {
1841 string = malloc(
sizeof(
char) * (data_len[i + 1] + 1));
1842 memcpy((
char *)
string, (
char *)par_data + count, data_len[i + 1]);
1843 string[data_len[i + 1]] =
'\0';
1844 *(
char **)SDDS_dataset->parameter[i] =
string;
1846 memcpy((
char *)(SDDS_dataset->parameter[i]), (
char *)par_data + count, data_len[i + 1]);
1847 count += data_len[i + 1];
1855 if (layout->n_arrays) {
1856 data_len = malloc(
sizeof(*data_len) * layout->n_arrays);
1857 if (MPI_dataset->myid == 0) {
1858 for (i = 0; i < layout->n_arrays; i++)
1859 data_len[i] = layout->array_definition[i].dimensions;
1861 MPI_Bcast(data_len, layout->n_arrays, MPI_INT64_T, 0, MPI_dataset->comm);
1862 for (i = 0; i < layout->n_arrays; i++) {
1863 type = layout->array_definition[i].type;
1867 if (MPI_dataset->myid == 0) {
1871 MPI_Bcast((
char *)SDDS_dataset->array[i].data, data_len[i] * size, MPI_BYTE, 0, MPI_dataset->comm);
1892 int32_t mpi_code, type = 0, retval, master_read;
1893 int64_t i, j, n_rows, prev_rows;
1894 MPI_DATASET *MPI_dataset;
1900 MPI_dataset = SDDS_dataset->MPI_dataset;
1901 master_read = MPI_dataset->master_read;
1903 if (SDDS_dataset->autoRecovered)
1905 if (SDDS_dataset->swapByteOrder) {
1911 if (MPI_dataset->file_offset >= MPI_dataset->file_size)
1912 return (SDDS_dataset->page_number = -1);
1913 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
1915 SDDS_SetError(
"Unable to set view for read binary page(1)");
1918#if defined(MASTER_READTITLE_ONLY)
1919 if (MPI_dataset->myid == 0)
1922#if defined(MASTER_READTITLE_ONLY)
1923 MPI_Bcast(&retval, 1, MPI_INT, 0, MPI_dataset->comm);
1927 return (SDDS_dataset->page_number = -1);
1929 SDDS_SetError(
"Unable to read the SDDS title (row number, parameter and/or array) data");
1932#if defined(MASTER_READTITLE_ONLY)
1936 if (MPI_dataset->total_rows < 0) {
1937 SDDS_SetError(
"Unable to read page--negative number of rows (SDDS_MPI_ReadBinaryPage)");
1942 return (SDDS_dataset->page_number = -1);
1946 n_rows = MPI_dataset->total_rows / MPI_dataset->n_processors;
1947 prev_rows = MPI_dataset->myid * n_rows;
1948 if (MPI_dataset->myid < (ID_offset = MPI_dataset->total_rows % (MPI_dataset->n_processors))) {
1950 prev_rows += MPI_dataset->myid;
1952 prev_rows += ID_offset;
1954 if (MPI_dataset->myid == 0)
1957 n_rows = MPI_dataset->total_rows / (MPI_dataset->n_processors - 1);
1958 prev_rows = (MPI_dataset->myid - 1) * n_rows;
1959 if (MPI_dataset->myid <= (ID_offset = MPI_dataset->total_rows % (MPI_dataset->n_processors - 1))) {
1961 prev_rows += (MPI_dataset->myid - 1);
1963 prev_rows += ID_offset;
1966 MPI_dataset->start_row = prev_rows;
1968 SDDS_SetError(
"Unable to read page--couldn't start page (SDDS_MPI_ReadBinaryPage)");
1971 offset = MPI_dataset->file_offset;
1972 fBuffer = &SDDS_dataset->fBuffer;
1974 if (SDDS_dataset->layout.data_mode.column_major) {
1976 for (i = 0; i < SDDS_dataset->layout.n_columns; i++) {
1977 type = SDDS_dataset->layout.column_definition[i].type;
1979 SDDS_SetError(
"Can not read string column from SDDS3 (SDDS_MPI_ReadBinaryPage");
1982 MPI_dataset->file_offset = offset + (MPI_Offset)prev_rows *
SDDS_type_size[type - 1];
1983 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
1984 SDDS_SetError(
"Unable to set view for read binary columns");
1987 if (!MPI_dataset->collective_io) {
1988 if ((mpi_code = MPI_File_read(MPI_dataset->MPI_file, (
char *)SDDS_dataset->data[i], n_rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
1989 SDDS_SetError(
"Unable to set view for read binary columns");
1993 if ((mpi_code = MPI_File_read_all(MPI_dataset->MPI_file, (
char *)SDDS_dataset->data[i], n_rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
1994 SDDS_SetError(
"Unable to set view for read binary columns");
1998 offset += (MPI_Offset)MPI_dataset->total_rows *
SDDS_type_size[type - 1];
2000 MPI_dataset->n_rows = SDDS_dataset->n_rows = n_rows;
2001 MPI_dataset->file_offset = offset;
2004 if (!fBuffer->buffer) {
2005 fBuffer->bufferSize = defaultReadBufferSize;
2006 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
2007 SDDS_SetError(
"Unable to do buffered read--allocation failure");
2010 fBuffer->bytesLeft = 0;
2011 fBuffer->data[0] = 0;
2013 if (fBuffer->bytesLeft > 0) {
2015 fBuffer->data[0] = 0;
2016 fBuffer->bytesLeft = 0;
2018 MPI_dataset->file_offset += (MPI_Offset)prev_rows * MPI_dataset->column_offset;
2019 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
2024 if (!master_read || !MPI_dataset->collective_io) {
2025 for (j = 0; j < n_rows; j++) {
2027 SDDS_dataset->n_rows = j;
2028 if (SDDS_dataset->autoRecover) {
2030 fprintf(stderr,
"Doing auto-read recovery\n");
2032 SDDS_dataset->autoRecovered = 1;
2034 return (SDDS_dataset->page_number = MPI_dataset->n_page);
2036 SDDS_SetError(
"Unable to read page--error reading data row (SDDS_MPI_ReadBinaryPage)");
2041 MPI_dataset->n_rows = SDDS_dataset->n_rows = j;
2043 MPI_dataset->n_rows = SDDS_dataset->n_rows = n_rows;
2047 MPI_dataset->file_offset = offset + MPI_dataset->total_rows * MPI_dataset->column_offset;
2049 MPI_dataset->n_page++;
2050 return (SDDS_dataset->page_number = MPI_dataset->n_page);
2085 if (SDDS_dataset->layout.disconnected) {
2086 SDDS_SetError(
"Can't read page--file is disconnected (SDDS_MPI_ReadNonNativePageSparse)");
2090 if (SDDS_dataset->original_layout.data_mode.mode == SDDS_ASCII) {
2093 }
else if (SDDS_dataset->original_layout.data_mode.mode == SDDS_BINARY) {
2098 SDDS_SetError(
"Unable to read page--unrecognized data mode (SDDS_MPI_ReadNonNativePageSparse)");
2116 int32_t ID_offset, mpi_code, master_read, type, retval;
2117 int64_t i, j, n_rows, total_rows, prev_rows;
2119 MPI_DATASET *MPI_dataset;
2123 MPI_dataset = SDDS_dataset->MPI_dataset;
2124 master_read = MPI_dataset->master_read;
2128 if (MPI_dataset->file_offset >= MPI_dataset->file_size)
2129 return (SDDS_dataset->page_number = -1);
2131 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
2133 SDDS_SetError(
"Unable to set view for read binary page(1)");
2137#if defined(MASTER_READTITLE_ONLY)
2138 if (MPI_dataset->myid == 0)
2141#if defined(MASTER_READTITLE_ONLY)
2142 MPI_Bcast(&retval, 1, MPI_INT, 0, MPI_dataset->comm);
2146 return (SDDS_dataset->page_number = -1);
2148 SDDS_SetError(
"Unable to read the SDDS title (row number, parameter and/or array) data");
2151#if defined(MASTER_READTITLE_ONLY)
2155 if (MPI_dataset->total_rows < 0) {
2156 SDDS_SetError(
"Unable to read page--negative number of rows (SDDS_MPI_ReadBinaryPage)");
2161 return (SDDS_dataset->page_number = -1);
2163 total_rows = MPI_dataset->total_rows;
2166 n_rows = total_rows / MPI_dataset->n_processors;
2167 prev_rows = MPI_dataset->myid * n_rows;
2168 if (MPI_dataset->myid < (ID_offset = total_rows % (MPI_dataset->n_processors))) {
2170 prev_rows += MPI_dataset->myid;
2172 prev_rows += ID_offset;
2174 if (MPI_dataset->myid == 0)
2177 n_rows = total_rows / (MPI_dataset->n_processors - 1);
2178 prev_rows = (MPI_dataset->myid - 1) * n_rows;
2179 if (MPI_dataset->myid <= (ID_offset = total_rows % (MPI_dataset->n_processors - 1))) {
2181 prev_rows += (MPI_dataset->myid - 1);
2183 prev_rows += ID_offset;
2186 MPI_dataset->start_row = prev_rows;
2188 SDDS_SetError(
"Unable to read page--couldn't start page (SDDS_MPI_ReadNonNativeBinaryPage)");
2192 offset = MPI_dataset->file_offset;
2193 fBuffer = &SDDS_dataset->fBuffer;
2194 if (SDDS_dataset->layout.data_mode.column_major) {
2196 for (i = 0; i < SDDS_dataset->layout.n_columns; i++) {
2197 type = SDDS_dataset->layout.column_definition[i].type;
2199 SDDS_SetError(
"Can not read string column from SDDS3 (SDDS_MPI_ReadBinaryPage");
2202 MPI_dataset->file_offset = offset + (MPI_Offset)prev_rows *
SDDS_type_size[type - 1];
2203 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
2204 SDDS_SetError(
"Unable to set view for read binary columns");
2207 if (!MPI_dataset->collective_io) {
2208 if ((mpi_code = MPI_File_read(MPI_dataset->MPI_file, (
char *)SDDS_dataset->data[i], n_rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
2209 SDDS_SetError(
"Unable to set view for read binary columns");
2213 if ((mpi_code = MPI_File_read_all(MPI_dataset->MPI_file, (
char *)SDDS_dataset->data[i], n_rows *
SDDS_type_size[type - 1], MPI_BYTE, &status)) != MPI_SUCCESS) {
2214 SDDS_SetError(
"Unable to set view for read binary columns");
2218 offset += (MPI_Offset)MPI_dataset->total_rows *
SDDS_type_size[type - 1];
2220 MPI_dataset->n_rows = SDDS_dataset->n_rows = n_rows;
2221 MPI_dataset->file_offset = offset;
2224 if (!fBuffer->buffer) {
2225 fBuffer->bufferSize = defaultReadBufferSize;
2226 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
2227 SDDS_SetError(
"Unable to do buffered read--allocation failure");
2230 fBuffer->bytesLeft = 0;
2231 fBuffer->data[0] = 0;
2233 if (fBuffer->bytesLeft > 0) {
2235 fBuffer->data[0] = 0;
2236 fBuffer->bytesLeft = 0;
2238 MPI_dataset->file_offset += (MPI_Offset)prev_rows * MPI_dataset->column_offset;
2239 if ((mpi_code = MPI_File_set_view(MPI_dataset->MPI_file, MPI_dataset->file_offset, MPI_BYTE, MPI_BYTE,
"native", MPI_INFO_NULL)) != MPI_SUCCESS) {
2244 if (!MPI_dataset->collective_io || !master_read) {
2245 for (j = 0; j < n_rows; j++) {
2247 SDDS_dataset->n_rows = j - 1;
2248 if (SDDS_dataset->autoRecover) {
2251 return (SDDS_dataset->page_number = MPI_dataset->n_page);
2253 SDDS_SetError(
"Unable to read page--error reading data row (SDDS_MPI_ReadNonNativeBinaryPage)");
2258 SDDS_dataset->n_rows = j;
2260 MPI_dataset->n_rows = SDDS_dataset->n_rows = n_rows;
2264 MPI_dataset->file_offset = offset + MPI_dataset->total_rows * MPI_dataset->column_offset;
2267 MPI_dataset->n_page++;
2268 MPI_Barrier(MPI_dataset->comm);
2269 return (SDDS_dataset->page_number = MPI_dataset->n_page);
2285 MPI_DATASET *MPI_dataset = NULL;
2289 MPI_dataset = SDDS_dataset->MPI_dataset;
2290 fBuffer = &(SDDS_dataset->titleBuffer);
2291 if (!fBuffer->buffer) {
2292 fBuffer->bufferSize = defaultTitleBufferSize;
2293 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
2294 SDDS_SetError(
"Unable to do buffered read--allocation failure(SDDS_MPI_ReadNonNativeBinaryTitle)");
2297 fBuffer->bytesLeft = 0;
2299 if (fBuffer->bytesLeft > 0) {
2301 fBuffer->data[0] = 0;
2302 fBuffer->bytesLeft = 0;
2304 if ((ret_val =
SDDS_MPI_BufferedRead((
void *)&(total_rows),
sizeof(int32_t), SDDS_dataset, fBuffer)) < 0)
2307 if (total_rows == INT32_MIN) {
2308 if ((ret_val =
SDDS_MPI_BufferedRead((
void *)&(MPI_dataset->total_rows),
sizeof(int64_t), SDDS_dataset, fBuffer)) < 0)
2311 MPI_dataset->total_rows = total_rows;
2316 SDDS_SetError(
"Unable to read page--couldn't start page (SDDS_MPI_BufferedReadNonNativeBinaryTitle)");
2321 SDDS_SetError(
"Unable to read page--parameter reading error (SDDS_MPI_BufferedNonNativeReadTitle)");
2326 SDDS_SetError(
"Unable to read page--array reading error (SDDS_MPI_BufferedNonNativeReadTitle)");
2347 MPI_Offset offset = 0;
2350 layout = &(SDDS_dataset->layout);
2351 offset +=
sizeof(int32_t);
2352 if (SDDS_dataset->n_rows > INT32_MAX) {
2353 offset +=
sizeof(int64_t);
2355 for (i = 0; i < layout->n_parameters; i++) {
2356 if (layout->parameter_definition[i].fixed_value)
2358 if (layout->parameter_definition[i].type ==
SDDS_STRING) {
2359 if (*(
char **)SDDS_dataset->parameter[i])
2360 offset +=
sizeof(int32_t) +
sizeof(
char) * strlen(*(
char **)SDDS_dataset->parameter[i]);
2362 offset +=
sizeof(int32_t);
2364 offset +=
SDDS_type_size[layout->parameter_definition[i].type - 1];
2367 for (i = 0; i < layout->n_arrays; i++) {
2368 if (!(SDDS_dataset->array[i].dimension)) {
2369 offset += layout->array_definition[i].dimensions *
sizeof(int32_t);
2372 offset +=
sizeof(*(SDDS_dataset->array)[i].dimension) * layout->array_definition[i].dimensions;
2373 if (layout->array_definition[i].type ==
SDDS_STRING) {
2374 for (j = 0; j < SDDS_dataset->array[i].elements; j++) {
2375 if (((
char **)SDDS_dataset->array[i].data)[j])
2376 offset +=
sizeof(int32_t) +
sizeof(
char) * strlen(((
char **)SDDS_dataset->array[i].data)[j]);
2378 offset +=
sizeof(int32_t);
2381 offset +=
SDDS_type_size[layout->array_definition[i].type - 1] * SDDS_dataset->array[i].elements;
2401 MPI_DATASET *MPI_dataset = NULL;
2405 MPI_dataset = SDDS_dataset->MPI_dataset;
2406 fBuffer = &(SDDS_dataset->titleBuffer);
2407 if (!fBuffer->buffer) {
2408 fBuffer->bufferSize = defaultTitleBufferSize;
2409 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
2410 SDDS_SetError(
"Unable to do buffered read--allocation failure(SDDS_MPI_ReadBinaryTitle)");
2413 fBuffer->bytesLeft = 0;
2415 if (fBuffer->bytesLeft > 0) {
2417 fBuffer->data[0] = 0;
2418 fBuffer->bytesLeft = 0;
2420 if ((ret_val =
SDDS_MPI_BufferedRead((
void *)&(total_rows),
sizeof(int32_t), SDDS_dataset, fBuffer)) < 0)
2422 if (total_rows == INT32_MIN) {
2423 if ((ret_val =
SDDS_MPI_BufferedRead((
void *)&(MPI_dataset->total_rows),
sizeof(int64_t), SDDS_dataset, fBuffer)) < 0)
2426 MPI_dataset->total_rows = total_rows;
2431 SDDS_SetError(
"Unable to read page--couldn't start page (SDDS_MPI_BufferedReadBinaryTitle)");
2436 SDDS_SetError(
"Unable to read page--parameter reading error (SDDS_MPI_BufferedReadTitle)");
2441 SDDS_SetError(
"Unable to read page--array reading error (SDDS_MPI_BufferedReadTitle)");
2460 MPI_DATASET *MPI_dataset;
2462 int32_t mpi_code, type, size;
2463 int64_t i, j, n_rows, min_rows, writeBytes;
2467 logDebug(
"SDDS_MPI_CollectiveWriteByRow", SDDS_dataset);
2470 MPI_dataset = SDDS_dataset->MPI_dataset;
2471 layout = &(SDDS_dataset->layout);
2472 fBuffer = &(SDDS_dataset->fBuffer);
2473 n_rows = SDDS_dataset->n_rows;
2474 MPI_Allreduce(&n_rows, &min_rows, 1, MPI_INT64_T, MPI_MIN, MPI_dataset->comm);
2475 for (i = 0; i < min_rows; i++) {
2476 for (j = 0; j < layout->n_columns; j++) {
2477 type = layout->column_definition[j].type;
2480 SDDS_SetError(
"Can not write binary string in collective io.");
2488 if ((writeBytes = fBuffer->bufferSize - fBuffer->bytesLeft)) {
2489 if (writeBytes < 0) {
2490 SDDS_SetError(
"Unable to flush buffer: negative byte count (SDDS_FlushBuffer).");
2493 if ((mpi_code = MPI_File_write_all(MPI_dataset->MPI_file, fBuffer->buffer, writeBytes, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
2494 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_FlushBuffer(MPI_File_write_at failed)", mpi_code, 0);
2497 fBuffer->bytesLeft = fBuffer->bufferSize;
2498 fBuffer->data = fBuffer->buffer;
2501 for (i = min_rows; i < n_rows; i++)
2521 MPI_DATASET *MPI_dataset;
2523 int32_t mpi_code, type, size;
2524 int64_t i, j, n_rows, min_rows, writeBytes;
2528 logDebug(
"SDDS_MPI_CollectiveWriteNonNativeByRow", SDDS_dataset);
2531 MPI_dataset = SDDS_dataset->MPI_dataset;
2532 layout = &(SDDS_dataset->layout);
2533 fBuffer = &(SDDS_dataset->fBuffer);
2534 n_rows = SDDS_dataset->n_rows;
2535 MPI_Allreduce(&n_rows, &min_rows, 1, MPI_INT64_T, MPI_MIN, MPI_dataset->comm);
2536 for (i = 0; i < min_rows; i++) {
2537 for (j = 0; j < layout->n_columns; j++) {
2538 type = layout->column_definition[j].type;
2541 SDDS_SetError(
"Can not write binary string in collective io.");
2549 if ((writeBytes = fBuffer->bufferSize - fBuffer->bytesLeft)) {
2550 if (writeBytes < 0) {
2551 SDDS_SetError(
"Unable to flush buffer: negative byte count (SDDS_FlushBuffer).");
2554 if ((mpi_code = MPI_File_write_all(MPI_dataset->MPI_file, fBuffer->buffer, writeBytes, MPI_BYTE, MPI_STATUS_IGNORE)) != MPI_SUCCESS) {
2555 SDDS_MPI_GOTO_ERROR(stderr,
"SDDS_MPI_FlushBuffer(MPI_File_write_at failed)", mpi_code, 0);
2558 fBuffer->bytesLeft = fBuffer->bufferSize;
2559 fBuffer->data = fBuffer->buffer;
2562 for (i = min_rows; i < n_rows; i++)
2584 MPI_DATASET *MPI_dataset;
2586 int64_t min_rows, i, j;
2589 MPI_dataset = SDDS_dataset->MPI_dataset;
2590 fBuffer = &(SDDS_dataset->fBuffer);
2591 layout = &(SDDS_dataset->layout);
2593 if (!MPI_dataset->master_read) {
2594 SDDS_SetError(
"Cannot read row with collective io when master is not reading the data.");
2597 min_rows = MPI_dataset->total_rows / MPI_dataset->n_processors;
2598 for (i = 0; i < min_rows; i++) {
2599 for (j = 0; j < layout->n_columns; j++) {
2600 type = layout->column_definition[j].type;
2603 SDDS_SetError(
"Can not write binary string in collective io.");
2610 for (i = min_rows; i < MPI_dataset->n_rows; i++)
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_MPI_WriteBinaryParameters(SDDS_DATASET *SDDS_dataset)
Write binary parameters of an SDDS dataset using MPI.
int32_t SDDS_MPI_ReadNonNativeBinaryRow(SDDS_DATASET *SDDS_dataset, int64_t row, int32_t skip)
Reads a single non-native binary row from a binary file buffer into the SDDS dataset using MPI.
int32_t SDDS_MPI_BufferedReadNonNativeBinaryTitle(SDDS_DATASET *SDDS_dataset)
Buffers and reads the non-native binary title data from an SDDS dataset using MPI.
int32_t SDDS_MPI_WriteBinaryRow(SDDS_DATASET *SDDS_dataset, int64_t row)
Write a binary row to an SDDS dataset using MPI.
int32_t SDDS_MPI_CollectiveWriteNonNativeByRow(SDDS_DATASET *SDDS_dataset)
Writes non-native binary SDDS dataset rows collectively by row using MPI parallel I/O.
char * SDDS_MPI_ReadBinaryString(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer, int32_t skip)
Read a binary string from the SDDS dataset using MPI.
int32_t SDDS_MPI_FlushBuffer(SDDS_DATASET *SDDS_dataset)
Flush the buffer by writing any remaining data to the MPI file.
int32_t SDDS_MPI_WriteBinaryString(SDDS_DATASET *SDDS_dataset, char *string)
Write a binary string to an SDDS dataset using MPI.
int32_t SDDS_MPI_WriteContinuousBinaryPage(SDDS_DATASET *SDDS_dataset)
Write a continuous binary page of the SDDS dataset using MPI.
int32_t SDDS_MPI_BufferedReadBinaryTitle(SDDS_DATASET *SDDS_dataset)
Buffers and reads the binary title data from an SDDS dataset using MPI.
void SDDS_StringTuncated(void)
Increment the truncated strings counter.
void SDDS_MPI_SetFileSync(short value)
Set the file synchronization flag.
int32_t SDDS_SetDefaultStringLength(int32_t newValue)
Set the default string length for SDDS.
int32_t SDDS_MPI_ReadNonNativePage(SDDS_DATASET *SDDS_dataset)
Reads a non-native binary page from an SDDS dataset using MPI parallel I/O.
int32_t SDDS_MPI_ReadBinaryParameters(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Read binary parameters from the SDDS dataset using MPI.
int32_t SDDS_MPI_WriteNonNativeBinaryRow(SDDS_DATASET *SDDS_dataset, int64_t row)
Write a non-native binary row to an SDDS dataset using MPI.
int32_t SDDS_MPI_BufferedRead(void *target, int64_t targetSize, SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Buffered read from an SDDS dataset using MPI.
MPI_Offset SDDS_MPI_GetTitleOffset(SDDS_DATASET *SDDS_dataset)
Calculates the byte offset for the title section in a non-native binary SDDS dataset.
int32_t SDDS_MPI_CollectiveReadByRow(SDDS_DATASET *SDDS_dataset)
Reads SDDS dataset rows collectively by row using MPI parallel I/O.
int64_t SDDS_MPI_CountRowsOfInterest(SDDS_DATASET *SDDS_dataset, int64_t start_row, int64_t end_row)
Count the number of rows marked as "of interest" within a specified range.
int32_t SDDS_SetDefaultWriteBufferSize(int32_t newSize)
Set the default write buffer size for SDDS.
MPI_Offset SDDS_MPI_Get_Column_Size(SDDS_DATASET *SDDS_dataset)
Get the total size of all columns in an SDDS dataset.
int32_t SDDS_CheckStringTruncated(void)
Check the number of truncated strings.
int64_t SDDS_MPI_GetTotalRows(SDDS_DATASET *SDDS_dataset)
Get the total number of rows across all MPI processes.
int32_t SDDS_MPI_WriteBinaryPage(SDDS_DATASET *SDDS_dataset)
Write an SDDS binary page using MPI.
int32_t SDDS_MPI_WriteNonNativeBinaryString(SDDS_DATASET *SDDS_dataset, char *string)
Write a non-native binary string to an SDDS dataset using MPI.
int32_t SDDS_MPI_ReadNonNativeBinaryParameters(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Read non-native binary parameters from the SDDS dataset using MPI.
int32_t SDDS_MPI_WriteBinaryArrays(SDDS_DATASET *SDDS_dataset)
Write binary arrays of an SDDS dataset using MPI.
int32_t SDDS_MPI_WriteNonNativeBinaryArrays(SDDS_DATASET *SDDS_dataset)
Write non-native binary arrays of an SDDS dataset using MPI.
int32_t SDDS_MPI_BufferedWrite(void *target, int64_t targetSize, SDDS_DATASET *SDDS_dataset)
Buffered write to an SDDS dataset using MPI.
int32_t SDDS_MPI_ReadBinaryArrays(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Read binary arrays from the SDDS dataset using MPI.
int32_t SDDS_MPI_WriteNonNativeBinaryParameters(SDDS_DATASET *SDDS_dataset)
Write non-native binary parameters of an SDDS dataset using MPI.
int32_t SDDS_MPI_ReadBinaryPage(SDDS_DATASET *SDDS_dataset)
Reads a binary page from an SDDS dataset using MPI parallel I/O.
int32_t SDDS_MPI_BufferedWriteAll(void *target, int64_t targetSize, SDDS_DATASET *SDDS_dataset)
Buffered write all to an SDDS dataset using MPI.
int32_t SDDS_SetDefaultReadBufferSize(int32_t newSize)
Set the default read buffer size for SDDS.
int32_t SDDS_MPI_BroadcastTitleData(SDDS_DATASET *SDDS_dataset)
Broadcasts the title data (parameters and arrays) of the SDDS dataset to all MPI processes.
int32_t SDDS_MPI_ReadBinaryRow(SDDS_DATASET *SDDS_dataset, int64_t row, int32_t skip)
Read a binary row from the SDDS dataset using MPI.
int32_t SDDS_MPI_BufferedReadAll(void *target, int64_t targetSize, SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Buffered read all from an SDDS dataset using MPI.
int32_t SDDS_MPI_ReadNonNativeBinaryArrays(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer)
Reads non-native binary arrays from a binary file buffer into the SDDS dataset using MPI.
int32_t SDDS_MPI_CollectiveWriteByRow(SDDS_DATASET *SDDS_dataset)
Writes SDDS dataset rows collectively by row using MPI parallel I/O.
int32_t SDDS_SetDefaultTitleBufferSize(int32_t newSize)
Set the default title buffer size for SDDS.
void SDDS_MPI_SetWriteKludgeUsleep(long value)
Set the write kludge usleep duration.
int32_t SDDS_MPI_ReadNonNativePageSparse(SDDS_DATASET *SDDS_dataset, uint32_t mode)
Reads a sparse non-native binary page from an SDDS dataset using MPI parallel I/O.
int32_t SDDS_MPI_WriteNonNativeBinaryPage(SDDS_DATASET *SDDS_dataset)
Write a non-native binary page of the SDDS dataset using MPI.
int32_t SDDS_MPI_ReadNonNativeBinaryPage(SDDS_DATASET *SDDS_dataset)
Reads a non-native binary page from an SDDS dataset using MPI parallel I/O.
char * SDDS_MPI_ReadNonNativeBinaryString(SDDS_DATASET *SDDS_dataset, SDDS_FILEBUFFER *fBuffer, int32_t skip)
Read a non-native binary string from the SDDS dataset using MPI.
int32_t SDDS_ScanData(char *string, int32_t type, int32_t field_length, void *data, int64_t index, int32_t is_parameter)
Scans a string and saves the parsed value into a data pointer according to the specified data type.
int32_t SDDS_SwapEndsColumnData(SDDS_DATASET *SDDSin)
Swaps the endianness of the column data in an SDDS dataset.
void SDDS_SwapLong64(int64_t *data)
Swaps the endianness of a 64-bit integer.
void SDDS_SetReadRecoveryMode(SDDS_DATASET *SDDS_dataset, int32_t mode)
Sets the read recovery mode for an SDDS dataset.
int32_t SDDS_SwapEndsArrayData(SDDS_DATASET *SDDSin)
Swaps the endianness of the array data in an SDDS dataset.
int32_t SDDS_SwapEndsParameterData(SDDS_DATASET *SDDSin)
Swaps the endianness of the parameter data in an SDDS dataset.
void SDDS_SwapLong(int32_t *data)
Swaps the endianness of a 32-bit integer.
int32_t SDDS_type_size[SDDS_NUM_TYPES]
Array of sizes for each supported data type.
int32_t SDDS_LengthenTable(SDDS_DATASET *SDDS_dataset, int64_t n_additional_rows)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
int32_t SDDS_FreeArrayDefinition(ARRAY_DEFINITION *source)
Frees memory allocated for an array definition.
int32_t SDDS_CheckDataset(SDDS_DATASET *SDDS_dataset, const char *caller)
Validates the SDDS dataset pointer.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
void SDDS_ClearErrors()
Clears all recorded error messages from the SDDS error stack.
ARRAY_DEFINITION * SDDS_CopyArrayDefinition(ARRAY_DEFINITION **target, ARRAY_DEFINITION *source)
Creates a copy of an array definition.
int32_t SDDS_IsBigEndianMachine()
Determines whether the current machine uses big-endian byte ordering.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
void SDDS_MPI_GOTO_ERROR(FILE *fp, char *str, int32_t mpierr, int32_t exit_code)
Handles MPI errors by printing an error message and optionally exiting.
#define SDDS_STRING
Identifier for the string data type.
void usleepSystemIndependent(long usec)
Sleep for a given number of microseconds, system-independently.