PLUTO
al_write_array_async.c
Go to the documentation of this file.
1 /* ///////////////////////////////////////////////////////////////////// */
2 /*!
3  \file
4  \brief ArrayLib routines for asynchronous MPI-IO
5 
6  ArrayLib routines for asynchronous MPI-IO
7 
8  \authors G. Muscianisi (g.muscianisi@cineca.it)
9 
10  \date Feb 28, 2012
11 */
12 /* ///////////////////////////////////////////////////////////////////// */
13 #include "al_hidden.h" /*I "al_hidden.h" I*/
14 #include <string.h>
15 
16 /*
17  The SZ structure stack is defined and maintained
18  in al_szptr_.c
19  Here we include an external reference to it in
20  order to be able to make internal references to it.
21 */
22 extern SZ *sz_stack[AL_MAX_ARRAYS];
23 extern int stack_ptr[AL_MAX_ARRAYS];
24 
25 /* ********************************************************************* */
26 int AL_Write_array_begin(void *va, int sz_ptr, int *output_stag, int *output_dump, int output_nvar)
27 /*!
28  * Write a distributed array to a parallel file by using
29  * asynchronous MPI-IO
30  *
31  * \param [in] buffer pointer to the buffer to write
32  * \param [in] sz_ptr integer pointer to the distributed
33  * array descriptor
34  * \param [in] output_stag vector sets to -1 for centred variables,
35  * and sets to 0,1,2 for staggered field in
36  * the x,y,z direction
37  * \param [in] output_dump vector sets to 1 if the variable has to
38  * be dumped, 0 in the contrary case
39  * \param [in] output_nvar total number of variables in PLUTO simulation
40  *********************************************************************** */
41 {
42  char *a;
43  register int i;
44  SZ *s;
45 
46  MPI_File ifp;
47 
48  int errcode;
49 
50  int myrank;
51  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
52 
53  a = (char *) va;
54 
55  s = sz_stack[sz_ptr];
56  ifp = s->ifp;
57 
58  /* Definition of a "global" filetype given by nvar times gsubarr for the MPI_File_set_view */
59  MPI_Datatype filetype_gsub_arr;
60  int count; /* number of variable to be dumped */
61  count=0;
62  for (i=0; i<output_nvar; i++){
63  if (!(output_dump[i]== 0)) count = count+1;
64  }
65  errcode = MPI_Type_contiguous(count, s->gsubarr, &filetype_gsub_arr);
66  errcode = MPI_Type_commit(&filetype_gsub_arr);
67 
68  /* Definition of a "global" datatype for the MPI_File_write_all_begin */
69  MPI_Datatype dtype_lsub_arr;
70 
71 #ifdef DEBUG
72  if(myrank==0) printf("count=%d, output_nvar=%d\n",count,output_nvar);
73 #endif
74 
75  int *block_leng, *displ;
76  int k=0;
77 
78  block_leng= (int*) malloc(count*sizeof(int));
79  displ=(int*) malloc(count*sizeof(int));
80 
81  for (i=0; i<output_nvar; i++){
82  if (!(output_dump[i]==0)){ /* cioè devo scrivere la variabile */
83  block_leng[k] = 1;
84  displ[k] = i;
85  k=k+1;
86  }
87  }
88 
89  errcode = MPI_Type_indexed(count,block_leng, displ, s->lsubarr, &dtype_lsub_arr);
90  errcode = MPI_Type_commit(&dtype_lsub_arr);
91 /*
92  MPI_Type_get_extent(dtype_lsub_arr,lb,extent);
93  printf("myid, %d, lb %d, extent %d of dtype_lsub_arr\n",myrank,lb,extent);
94 */
95 /* DIAGNOSTICS */
96 #ifdef DEBUG
97  if (myrank==0) {
98  for (i=0; i<output_nvar; i++) printf("myid, %d, count %d, output_nvar %d, i %d, output_dump[i] %d, block_leng[i] %d, displ[i] %d\n", myrank, count, output_nvar, i, output_dump[i], block_leng[i], displ[i]);
99  }
100 #endif
101 
102  free(block_leng);
103  free(displ);
104  MPI_Barrier(s->comm);
105 
106  /* Setting of the new view. Each procs see all the file at the beginning of the writing */
107  errcode = MPI_File_set_view(ifp, 0, MPI_BYTE, filetype_gsub_arr,
108  "native", MPI_INFO_NULL);
109  MPI_Type_free(&filetype_gsub_arr);
110 
111 /* DIAGNOSTICS */
112 #ifdef DEBUG
113  int len;
114  char es[MPI_MAX_ERROR_STRING];
115  MPI_Error_string(errcode, es, &len);
116  printf("myid %d, Errcode from MPI_File_set_view: %d | %s\n", myrank, errcode, es);
117 #endif
118 
119  errcode = MPI_File_write_all_begin(ifp, va, 1,dtype_lsub_arr);
120  MPI_Type_free(&dtype_lsub_arr);
121 
122 /* DIAGNOSTICS */
123 #ifdef DEBUG
124  printf("myid %d\n", myrank);
125  MPI_Error_string(errcode, es, &len);
126  printf("myid %d, Errcode from MPI_File_write_all_begin: %d | %s\n", myrank, errcode, es);
127 #endif
128 
129  return (int) AL_SUCCESS;
130 }
131 
132 /* ********************************************************************* */
133 int AL_Write_array_end(void *va, int sz_ptr)
134 /*!
135  * Completition of writing of a distributed array to a
136  * parallel file by using asynchronous MPI-IO
137  *
138  * \param [in] buffer pointer to the buffer to write
139  * \param [in] sz_ptr integer pointer to the distributed array descriptor
140  *********************************************************************** */
141 {
142  char *a;
143  SZ *s;
144 
145  MPI_Status status;
146 
147  MPI_File ifp;
148 
149  int errcode;
150 
151  int myrank;
152  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
153 
154  a = (char *) va;
155 
156  s = sz_stack[sz_ptr];
157  ifp = s->ifp;
158 
159  errcode=MPI_File_write_all_end(ifp, a, &status);
160 
161  /* DIAGNOSTICS */
162 #ifdef DEBUG
163  int len;
164  char es[128];
165  if( errcode ){
166  MPI_Error_string(errcode, es, &len);
167  printf("Errcode from MPI_File_write_all_end: %d | %s\n", errcode, es);
168  }
169 #endif
170 
171  MPI_File_sync(ifp);
172 
173  return (int) AL_SUCCESS;
174 }
static double a
Definition: init.c:135
#define AL_SUCCESS
Definition: al_codes.h:32
MPI_Datatype gsubarr
Definition: al_hidden.h:110
int stack_ptr[AL_MAX_ARRAYS]
Definition: al_szptr_.c:26
MPI_File ifp
Definition: al_hidden.h:117
MPI_Datatype lsubarr
Definition: al_hidden.h:111
int k
Definition: analysis.c:2
int AL_Write_array_end(void *va, int sz_ptr)
#define s
int i
Definition: analysis.c:2
MPI_Comm comm
Definition: al_hidden.h:47
SZ * sz_stack[AL_MAX_ARRAYS]
Definition: al_szptr_.c:18
Definition: al_hidden.h:38
Internal include file for the ArrayLib.
int AL_Write_array_begin(void *va, int sz_ptr, int *output_stag, int *output_dump, int output_nvar)
#define AL_MAX_ARRAYS
Definition: al_codes.h:21