455 MPI_DATASET *MPI_dataset;
457 char buf[2048], *field = NULL;
458 int32_t i, data_mode;
459 char *outputEndianess = NULL;
463 logDebug(
"SDDS_MPI_WriteLayout", SDDS_dataset);
466 MPI_dataset = SDDS_dataset->MPI_dataset;
467 MPI_File_set_view(MPI_dataset->MPI_file, 0, MPI_CHAR, MPI_CHAR,
"native", MPI_INFO_NULL);
472 layout = &SDDS_dataset->layout;
473 if (SDDS_dataset->layout.disconnected) {
474 SDDS_SetError(
"Can't write layout--file is disconnected (SDDS_MPI_WriteLayout)");
477 MPI_dataset->file_offset = 0;
478 if (layout->layout_written) {
479 SDDS_SetError(
"Can't write layout--already written to file (SDDS_MPI_WriteLayout)");
483 if ((outputEndianess = getenv(
"SDDS_OUTPUT_ENDIANESS"))) {
484 if (strncmp(outputEndianess,
"big", 3) == 0)
485 layout->byteOrderDeclared = SDDS_BIGENDIAN;
486 else if (strncmp(outputEndianess,
"little", 6) == 0)
487 layout->byteOrderDeclared = SDDS_LITTLEENDIAN;
490 if (!layout->byteOrderDeclared)
493 fBuffer = &(SDDS_dataset->fBuffer);
495 if (MPI_dataset->myid == 0) {
496 if (!fBuffer->buffer) {
498 if (!(fBuffer->buffer = fBuffer->data =
SDDS_Malloc(
sizeof(
char) * (fBuffer->bufferSize + 1)))) {
499 SDDS_SetError(
"Unable to do buffered read--allocation failure (SDDS_MPI_WriteLayout)");
502 fBuffer->bytesLeft = fBuffer->bufferSize;
503 fBuffer->data[0] = 0;
507 for (i = 0; i < layout->n_parameters; i++) {
508 if ((layout->parameter_definition[i].type ==
SDDS_ULONG) || (layout->parameter_definition[i].type ==
SDDS_USHORT)) {
513 for (i = 0; i < layout->n_arrays; i++) {
514 if ((layout->array_definition[i].type ==
SDDS_ULONG) || (layout->array_definition[i].type ==
SDDS_USHORT)) {
519 for (i = 0; i < layout->n_columns; i++) {
520 if ((layout->column_definition[i].type ==
SDDS_ULONG) || (layout->column_definition[i].type ==
SDDS_USHORT)) {
525 if ((layout->data_mode.column_major) && (layout->data_mode.mode == SDDS_BINARY)) {
528 for (i = 0; i < layout->n_parameters; i++) {
534 for (i = 0; i < layout->n_arrays; i++) {
540 for (i = 0; i < layout->n_columns; i++) {
546 if ((LDBL_DIG != 18) && (layout->version == 4)) {
547 SDDS_SetError(
"Operating system does not support 80bit float variables used by SDDS_LONGDOUBLE (SDDS_MPI_WriteLayout)");
550 for (i = 0; i < layout->n_parameters; i++) {
551 if ((layout->parameter_definition[i].type ==
SDDS_ULONG64) || (layout->parameter_definition[i].type ==
SDDS_LONG64)) {
556 for (i = 0; i < layout->n_arrays; i++) {
562 for (i = 0; i < layout->n_columns; i++) {
563 if ((layout->column_definition[i].type ==
SDDS_ULONG64) || (layout->column_definition[i].type ==
SDDS_LONG64)) {
570 sprintf(buf,
"SDDS%" PRId32
"\n", layout->version);
572 MPI_dataset->file_offset += strlen(buf) *
sizeof(char);
573 data_mode = SDDS_dataset->layout.data_mode.mode;
575 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
578 if (data_mode == SDDS_BINARY) {
579 if (layout->byteOrderDeclared == SDDS_BIGENDIAN)
580 sprintf(buf,
"!# big-endian\n");
582 sprintf(buf,
"!# little-endian\n");
584 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
587 MPI_dataset->file_offset += strlen(buf) *
sizeof(char);
589 if (SDDS_dataset->layout.data_mode.fixed_row_count) {
590 sprintf(buf,
"!# fixed-rowcount\n");
592 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
595 MPI_dataset->file_offset += strlen(buf) *
sizeof(char);
600 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
603 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
608 for (i = 0; i < layout->n_parameters; i++) {
611 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
614 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
618 for (i = 0; i < layout->n_arrays; i++) {
621 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
624 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
628 for (i = 0; i < layout->n_columns; i++) {
631 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
634 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
639#if RW_ASSOCIATES != 0
640 for (i = 0; i < layout->n_associates; i++) {
643 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
646 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
653 fprintf(stderr,
"SDDS_MPI_WriteLayout(error1): Unable to write layout.\n");
656 MPI_dataset->file_offset += strlen(field) *
sizeof(char);
661 layout->layout_written = 1;
662 if (MPI_dataset->myid == 0) {
666 SDDS_dataset->original_layout.version = layout->version;
772 MPI_DATASET *MPI_dataset;
778 logDebug(
"SDDS_MPI_Terminate", SDDS_dataset);
781 MPI_dataset = SDDS_dataset->MPI_dataset;
782 layout = &(SDDS_dataset->original_layout);
785 if (SDDS_dataset->pagecount_offset)
786 free(SDDS_dataset->pagecount_offset);
787 if (SDDS_dataset->row_flag)
788 free(SDDS_dataset->row_flag);
789 if (SDDS_dataset->column_order)
790 free(SDDS_dataset->column_order);
791 if (SDDS_dataset->column_flag)
792 free(SDDS_dataset->column_flag);
793 if (SDDS_dataset->fBuffer.buffer)
794 free(SDDS_dataset->fBuffer.buffer);
795 if (SDDS_dataset->titleBuffer.buffer)
796 free(SDDS_dataset->titleBuffer.buffer);
797 if (SDDS_dataset->parameter) {
798 for (i = 0; i < layout->n_parameters; i++) {
799 if (layout->parameter_definition[i].type ==
SDDS_STRING && *(
char **)(SDDS_dataset->parameter[i]))
800 free(*(
char **)(SDDS_dataset->parameter[i]));
801 if (SDDS_dataset->parameter[i])
802 free(SDDS_dataset->parameter[i]);
804 free(SDDS_dataset->parameter);
806 if (SDDS_dataset->array) {
807 for (i = 0; i < layout->n_arrays; i++) {
808 if (layout->array_definition[i].type ==
SDDS_STRING && !(terminateMode & TERMINATE_DONT_FREE_ARRAY_STRINGS)) {
809 for (j = 0; j < SDDS_dataset->array[i].elements; j++)
810 if (((
char **)SDDS_dataset->array[i].data)[j])
811 free(((
char **)SDDS_dataset->array[i].data)[j]);
821 if (SDDS_dataset->array[i].data)
822 free(SDDS_dataset->array[i].data);
824 if (SDDS_dataset->array[i].pointer && SDDS_dataset->array[i].definition->dimensions != 1)
825 free(SDDS_dataset->array[i].pointer);
826 if (SDDS_dataset->array[i].dimension)
827 free(SDDS_dataset->array[i].dimension);
829 if (SDDS_dataset->array[i].definition && SDDS_dataset->array[i].definition->name) {
830 if (SDDS_dataset->array[i].definition->name != layout->array_definition[i].name)
833 SDDS_dataset->array[i].definition = NULL;
835 free(SDDS_dataset->array);
837 if (SDDS_dataset->data) {
838 for (i = 0; i < layout->n_columns; i++)
839 if (SDDS_dataset->data[i]) {
840 if (layout->column_definition[i].type ==
SDDS_STRING && !(terminateMode & TERMINATE_DONT_FREE_TABLE_STRINGS)) {
841 ptr = (
char **)SDDS_dataset->data[i];
842 for (j = 0; j < SDDS_dataset->n_rows_allocated; j++, ptr++)
846 free(SDDS_dataset->data[i]);
848 free(SDDS_dataset->data);
850 if (layout->description)
851 free(layout->description);
852 if (layout->contents == (&SDDS_dataset->layout)->contents)
853 (&SDDS_dataset->layout)->contents = NULL;
854 if (layout->contents)
855 free(layout->contents);
856 if (layout->filename)
857 free(layout->filename);
858 if (layout->column_definition) {
859 for (i = 0; i < layout->n_columns; i++) {
860 if (layout->column_index[i])
861 free(layout->column_index[i]);
862 if (layout->column_definition[i].name)
863 free(layout->column_definition[i].name);
864 if (layout->column_definition[i].symbol)
865 free(layout->column_definition[i].symbol);
866 if (layout->column_definition[i].units)
867 free(layout->column_definition[i].units);
868 if (layout->column_definition[i].description)
869 free(layout->column_definition[i].description);
870 if (layout->column_definition[i].format_string)
871 free(layout->column_definition[i].format_string);
873 free(layout->column_definition);
874 free(layout->column_index);
876 if (layout->parameter_definition) {
877 for (i = 0; i < layout->n_parameters; i++) {
878 if (layout->parameter_index[i])
879 free(layout->parameter_index[i]);
880 if (layout->parameter_definition[i].name)
881 free(layout->parameter_definition[i].name);
882 if (layout->parameter_definition[i].symbol)
883 free(layout->parameter_definition[i].symbol);
884 if (layout->parameter_definition[i].units)
885 free(layout->parameter_definition[i].units);
886 if (layout->parameter_definition[i].description)
887 free(layout->parameter_definition[i].description);
888 if (layout->parameter_definition[i].format_string)
889 free(layout->parameter_definition[i].format_string);
890 if (layout->parameter_definition[i].fixed_value)
891 free(layout->parameter_definition[i].fixed_value);
893 free(layout->parameter_definition);
894 free(layout->parameter_index);
896 if (layout->array_definition) {
897 for (i = 0; i < layout->n_arrays; i++) {
898 if (layout->array_index[i])
899 free(layout->array_index[i]);
900 if (layout->array_definition[i].name)
901 free(layout->array_definition[i].name);
902 if (layout->array_definition[i].symbol)
903 free(layout->array_definition[i].symbol);
904 if (layout->array_definition[i].units)
905 free(layout->array_definition[i].units);
906 if (layout->array_definition[i].description)
907 free(layout->array_definition[i].description);
908 if (layout->array_definition[i].format_string)
909 free(layout->array_definition[i].format_string);
910 if (layout->array_definition[i].group_name)
911 free(layout->array_definition[i].group_name);
913 free(layout->array_definition);
914 free(layout->array_index);
916 if (layout->associate_definition) {
917 for (i = 0; i < layout->n_associates; i++) {
918 if (layout->associate_definition[i].name)
919 free(layout->associate_definition[i].name);
920 if (layout->associate_definition[i].filename)
921 free(layout->associate_definition[i].filename);
922 if (layout->associate_definition[i].path)
923 free(layout->associate_definition[i].path);
924 if (layout->associate_definition[i].description)
925 free(layout->associate_definition[i].description);
926 if (layout->associate_definition[i].contents)
927 free(layout->associate_definition[i].contents);
929 free(layout->associate_definition);
932 layout = &SDDS_dataset->layout;
933 if (layout->contents)
934 free(layout->contents);
935 if (layout->column_definition)
936 free(layout->column_definition);
937 if (layout->array_definition)
938 free(layout->array_definition);
939 if (layout->associate_definition)
940 free(layout->associate_definition);
941 if (layout->parameter_definition)
942 free(layout->parameter_definition);
943 if (layout->column_index)
944 free(layout->column_index);
945 if (layout->parameter_index)
946 free(layout->parameter_index);
947 if (layout->array_index)
948 free(layout->array_index);
952 fprintf(stderr,
"done\n");
954 MPI_File_close(&(MPI_dataset->MPI_file));