00001
00002
00003
00004
00005
00006
00007
00008 #include <assert.h>
00009 #include "adio.h"
00010 #include "adio_extern.h"
00011 #ifdef AGGREGATION_PROFILE
00012 #include "mpe.h"
00013 #endif
00014
00015
00016
00017
00018
00019
00020 #define COUNT_EXCH 0
00021 #define BLOCK_LENS 1
00022 #define INDICES 2
00023 #define FPIND_DISP_OFF_SZ 3
00024
00025
00026 typedef struct {
00027 int count;
00028 ADIO_Offset fp_ind;
00029 ADIO_Offset disp;
00030 ADIO_Offset byte_off;
00031 ADIO_Offset sz;
00032 ADIO_Offset ext;
00033 ADIO_Offset type_sz;
00034 } amount_and_extra_data_t;
00035
00036
00037 void ADIOI_Print_flatlist_node(ADIOI_Flatlist_node *flatlist_node_p)
00038 {
00039 int i;
00040 if (flatlist_node_p == NULL)
00041 {
00042 fprintf(stderr, "print flatlist node of NULL ptr\n");
00043 return;
00044 }
00045 fprintf(stderr, "print flatlist node count = %d (idx,blocklen)\n",
00046 flatlist_node_p->count);
00047 for (i = 0; i < flatlist_node_p->count; i++)
00048 {
00049 if (i % 5 == 0 && i != 0)
00050 {
00051 fprintf(stderr, "%d=(%Ld,%Ld)\n", i, flatlist_node_p->indices[i],
00052 flatlist_node_p->blocklens[i]);
00053 }
00054 else
00055 fprintf(stderr, "%d=(%Ld,%Ld) ", i, flatlist_node_p->indices[i],
00056 flatlist_node_p->blocklens[i]);
00057 }
00058 fprintf(stderr, "\n");
00059 }
00060
00061
00062
00063 ADIOI_Flatlist_node * ADIOI_Add_contig_flattened(MPI_Datatype contig_type)
00064 {
00065 int contig_type_sz = -1;
00066 ADIOI_Flatlist_node *flat_node_p = CtvAccess(ADIOI_Flatlist);
00067
00068
00069
00070 while (flat_node_p->next)
00071 {
00072 if (flat_node_p->type == contig_type)
00073 return flat_node_p;
00074 flat_node_p = flat_node_p->next;
00075 }
00076 if (flat_node_p->type == contig_type)
00077 return flat_node_p;
00078
00079 MPI_Type_size(contig_type, &contig_type_sz);
00080 if ((flat_node_p->next = (ADIOI_Flatlist_node *) ADIOI_Malloc
00081 (sizeof(ADIOI_Flatlist_node))) == NULL)
00082 {
00083 fprintf(stderr, "ADIOI_Add_contig_flattened: malloc next failed\n");
00084 }
00085 flat_node_p = flat_node_p->next;
00086 flat_node_p->type = contig_type;
00087 if ((flat_node_p->blocklens = (ADIO_Offset *) ADIOI_Malloc(sizeof(ADIO_Offset))) == NULL)
00088 {
00089 fprintf(stderr, "ADIOI_Flatlist_node: malloc blocklens failed\n");
00090 }
00091 if ((flat_node_p->indices = (ADIO_Offset *)
00092 ADIOI_Malloc(sizeof(ADIO_Offset))) == NULL)
00093 {
00094 fprintf(stderr, "ADIOI_Flatlist_node: malloc indices failed\n");
00095 }
00096 flat_node_p->blocklens[0] = contig_type_sz;
00097 flat_node_p->indices[0] = 0;
00098 flat_node_p->count = 1;
00099 flat_node_p->next = NULL;
00100 return flat_node_p;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 void ADIOI_Exch_file_views(int myrank, int nprocs, int file_ptr_type,
00110 ADIO_File fd, int count,
00111 MPI_Datatype datatype, ADIO_Offset off,
00112 view_state *my_mem_view_state_arr,
00113 view_state *agg_file_view_state_arr,
00114 view_state *client_file_view_state_arr)
00115 {
00116
00117
00118
00119
00120
00121
00122 int i = -1, j = -1;
00123 amount_and_extra_data_t *send_count_arr = NULL;
00124 amount_and_extra_data_t *recv_count_arr = NULL;
00125 int send_req_arr_sz = 0;
00126 int recv_req_arr_sz = 0;
00127 MPI_Request *send_req_arr = NULL, *recv_req_arr = NULL;
00128 MPI_Status *statuses = NULL;
00129 ADIO_Offset disp_off_sz_ext_typesz[6];
00130 MPI_Aint memtype_extent, filetype_extent;
00131 int ret = -1;
00132
00133
00134 ADIOI_Flatlist_node *flat_mem_p = NULL, *flat_file_p = NULL;
00135 int memtype_sz = -1;
00136 int memtype_is_contig = -1, filetype_is_contig = -1;
00137 int filetype_sz = -1;
00138
00139 #ifdef AGGREGATION_PROFILE
00140 MPE_Log_event (5014, 0, NULL);
00141 #endif
00142
00143
00144
00145 MPI_Type_size(datatype, &memtype_sz);
00146 MPI_Type_extent(datatype, &memtype_extent);
00147 if (memtype_sz == memtype_extent) {
00148 memtype_is_contig = 1;
00149 flat_mem_p = ADIOI_Add_contig_flattened(datatype);
00150 flat_mem_p->blocklens[0] = memtype_sz*count;
00151 }
00152 else {
00153 ADIOI_Flatten_datatype(datatype);
00154 flat_mem_p = CtvAccess(ADIOI_Flatlist);
00155 while (flat_mem_p->type != datatype)
00156 flat_mem_p = flat_mem_p->next;
00157 }
00158
00159 MPI_Type_extent(fd->filetype, &filetype_extent);
00160 MPI_Type_size(fd->filetype, &filetype_sz);
00161 if (filetype_extent == filetype_sz) {
00162 filetype_is_contig = 1;
00163 flat_file_p = ADIOI_Add_contig_flattened(fd->filetype);
00164 flat_file_p->blocklens[0] = memtype_sz*count;
00165 filetype_extent = memtype_sz*count;
00166 filetype_sz = filetype_extent;
00167 }
00168 else {
00169 flat_file_p = CtvAccess(ADIOI_Flatlist);
00170 while (flat_file_p->type != fd->filetype)
00171 flat_file_p = flat_file_p->next;
00172 }
00173
00174 disp_off_sz_ext_typesz[0] = fd->fp_ind;
00175 disp_off_sz_ext_typesz[1] = fd->disp;
00176 disp_off_sz_ext_typesz[2] = off;
00177 disp_off_sz_ext_typesz[3] = memtype_sz*count;
00178 disp_off_sz_ext_typesz[4] = (ADIO_Offset) filetype_extent;
00179 disp_off_sz_ext_typesz[5] = (ADIO_Offset) filetype_sz;
00180
00181 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00182 recv_count_arr = ADIOI_Calloc(nprocs, sizeof(amount_and_extra_data_t));
00183 send_count_arr = ADIOI_Calloc(nprocs, sizeof(amount_and_extra_data_t));
00184 } else {
00185 send_count_arr = ADIOI_Calloc(fd->hints->cb_nodes,
00186 sizeof(amount_and_extra_data_t));
00187
00188
00189 if (fd->is_agg) {
00190 recv_count_arr = ADIOI_Calloc(nprocs,
00191 sizeof(amount_and_extra_data_t));
00192 recv_req_arr = ADIOI_Malloc (nprocs * sizeof(MPI_Request));
00193 for (i=0; i < nprocs; i++)
00194 MPI_Irecv (&recv_count_arr[i], sizeof(amount_and_extra_data_t),
00195 MPI_BYTE, i, COUNT_EXCH, fd->comm, &recv_req_arr[i]);
00196 }
00197
00198
00199 send_req_arr = ADIOI_Calloc (fd->hints->cb_nodes, sizeof(MPI_Request));
00200 for (i=0; i < fd->hints->cb_nodes; i++) {
00201 send_count_arr[i].count = flat_file_p->count;
00202 send_count_arr[i].fp_ind = disp_off_sz_ext_typesz[0];
00203 send_count_arr[i].disp = disp_off_sz_ext_typesz[1];
00204 send_count_arr[i].byte_off = disp_off_sz_ext_typesz[2];
00205 send_count_arr[i].sz = disp_off_sz_ext_typesz[3];
00206 send_count_arr[i].ext = disp_off_sz_ext_typesz[4];
00207 send_count_arr[i].type_sz = disp_off_sz_ext_typesz[5];
00208 MPI_Isend (&send_count_arr[i], sizeof(amount_and_extra_data_t),
00209 MPI_BYTE, fd->hints->ranklist[i], COUNT_EXCH, fd->comm,
00210 &send_req_arr[i]);
00211 }
00212 }
00213
00214
00215
00216
00217
00218
00219 if (memtype_is_contig) {
00220
00221
00222 memtype_sz *= count;
00223 memtype_extent = memtype_sz;
00224 }
00225
00226 for (i = 0; i < fd->hints->cb_nodes; i++)
00227 {
00228 int tmp_agg_idx = fd->hints->ranklist[i];
00229 memset(&(my_mem_view_state_arr[tmp_agg_idx]), 0, sizeof(view_state));
00230 my_mem_view_state_arr[tmp_agg_idx].sz =
00231 disp_off_sz_ext_typesz[3];
00232 my_mem_view_state_arr[tmp_agg_idx].ext =
00233 (ADIO_Offset) memtype_extent;
00234 my_mem_view_state_arr[tmp_agg_idx].type_sz =
00235 (ADIO_Offset) memtype_sz;
00236 my_mem_view_state_arr[tmp_agg_idx].flat_type_p = flat_mem_p;
00237 ADIOI_init_view_state(file_ptr_type,
00238 1,
00239 &(my_mem_view_state_arr[tmp_agg_idx]),
00240 TEMP_OFF);
00241 ADIOI_init_view_state(file_ptr_type,
00242 1,
00243 &(my_mem_view_state_arr[tmp_agg_idx]),
00244 REAL_OFF);
00245
00246 memset(&(agg_file_view_state_arr[tmp_agg_idx]), 0, sizeof(view_state));
00247 agg_file_view_state_arr[tmp_agg_idx].fp_ind =
00248 disp_off_sz_ext_typesz[0];
00249 agg_file_view_state_arr[tmp_agg_idx].disp =
00250 disp_off_sz_ext_typesz[1];
00251 agg_file_view_state_arr[tmp_agg_idx].byte_off =
00252 disp_off_sz_ext_typesz[2];
00253 agg_file_view_state_arr[tmp_agg_idx].sz =
00254 disp_off_sz_ext_typesz[3];
00255 agg_file_view_state_arr[tmp_agg_idx].ext =
00256 disp_off_sz_ext_typesz[4];
00257 agg_file_view_state_arr[tmp_agg_idx].type_sz =
00258 disp_off_sz_ext_typesz[5];
00259 agg_file_view_state_arr[tmp_agg_idx].flat_type_p = flat_file_p;
00260
00261 ADIOI_init_view_state(file_ptr_type,
00262 1,
00263 &(agg_file_view_state_arr[tmp_agg_idx]),
00264 TEMP_OFF);
00265 ADIOI_init_view_state(file_ptr_type,
00266 1,
00267 &(agg_file_view_state_arr[tmp_agg_idx]),
00268 REAL_OFF);
00269
00270 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00271 send_count_arr[tmp_agg_idx].count = flat_file_p->count;
00272 send_count_arr[tmp_agg_idx].fp_ind = disp_off_sz_ext_typesz[0];
00273 send_count_arr[tmp_agg_idx].disp = disp_off_sz_ext_typesz[1];
00274 send_count_arr[tmp_agg_idx].byte_off = disp_off_sz_ext_typesz[2];
00275 send_count_arr[tmp_agg_idx].sz = disp_off_sz_ext_typesz[3];
00276 send_count_arr[tmp_agg_idx].ext = disp_off_sz_ext_typesz[4];
00277 send_count_arr[tmp_agg_idx].type_sz = disp_off_sz_ext_typesz[5];
00278 }
00279 }
00280
00281 #ifdef DEBUG2
00282 fprintf(stderr, "my own flattened memtype: ");
00283 ADIOI_Print_flatlist_node(flat_mem_p);
00284 fprintf(stderr, "my own flattened filetype: ");
00285 ADIOI_Print_flatlist_node(flat_file_p);
00286 #endif
00287
00288 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00289 ret = MPI_Alltoall(send_count_arr, sizeof(amount_and_extra_data_t),
00290 MPI_BYTE,
00291 recv_count_arr, sizeof(amount_and_extra_data_t),
00292 MPI_BYTE, fd->comm);
00293 if (ret != MPI_SUCCESS)
00294 {
00295 fprintf(stderr, "ADIOI_Exchange_file_views: MPI_Alltoall failed "
00296 "with error %d", ret);
00297 return;
00298 }
00299 } else {
00300 statuses = (MPI_Status *) ADIOI_Malloc(1 + nprocs * sizeof(MPI_Status));
00301 if (fd->is_agg) {
00302 MPI_Waitall(nprocs, recv_req_arr, statuses);
00303 ADIOI_Free(recv_req_arr);
00304 }
00305 MPI_Waitall(fd->hints->cb_nodes, send_req_arr, statuses);
00306 ADIOI_Free(statuses);
00307 ADIOI_Free(send_req_arr);
00308 }
00309 #ifdef DEBUG2
00310 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00311 fprintf(stderr, "send_count_arr:");
00312 for (i = 0; i < nprocs; i++)
00313 {
00314 fprintf(stderr, "[%d]=%d ", i, send_count_arr[i].count);
00315 }
00316 fprintf(stderr, "\n");
00317 fprintf(stderr, "recv_count_arr:");
00318 for (i = 0; i < nprocs; i++)
00319 {
00320 fprintf(stderr, "[%d]=%d ", i, recv_count_arr[i].count);
00321 }
00322 fprintf(stderr, "\n");
00323 } else {
00324 fprintf(stderr, "send_count_arr:");
00325 for (i = 0; i < fd->hints->cb_nodes; i++)
00326 {
00327 fprintf(stderr, "[%d]=%d ", i, send_count_arr[i].count);
00328 }
00329 fprintf(stderr, "\n");
00330 if (fd->is_agg) {
00331 fprintf(stderr, "recv_count_arr:");
00332 for (i = 0; i < nprocs; i++)
00333 {
00334 fprintf(stderr, "[%d]=%d ", i, recv_count_arr[i].count);
00335 }
00336 fprintf(stderr, "\n");
00337 }
00338 }
00339 #endif
00340
00341 if (fd->hints->cb_alltoall == ADIOI_HINT_DISABLE) {
00342 for (i=0; i < fd->hints->cb_nodes; i++)
00343 if (send_count_arr[i].count > 0)
00344 send_req_arr_sz++;
00345 }
00346
00347 for (i = 0; i < nprocs; i++)
00348 {
00349 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00350 if (send_count_arr[i].count > 0)
00351 send_req_arr_sz++;
00352 }
00353
00354 if (fd->is_agg) {
00355 if (recv_count_arr[i].count > 0)
00356 {
00357 if ((client_file_view_state_arr[i].flat_type_p =
00358 (ADIOI_Flatlist_node *) ADIOI_Malloc(
00359 sizeof(ADIOI_Flatlist_node))) == NULL)
00360 {
00361 fprintf(stderr, "ADIOI_Exchange_file_views: malloc "
00362 "flat_type_p failed\n");
00363 }
00364 client_file_view_state_arr[i].flat_type_p->count =
00365 recv_count_arr[i].count;
00366 client_file_view_state_arr[i].flat_type_p->indices =
00367 (ADIO_Offset *) ADIOI_Calloc(recv_count_arr[i].count,
00368 sizeof(ADIO_Offset));
00369 client_file_view_state_arr[i].flat_type_p->blocklens =
00370 (ADIO_Offset *) ADIOI_Calloc(recv_count_arr[i].count,
00371 sizeof(ADIO_Offset));
00372
00373
00374 memcpy (&client_file_view_state_arr[i].fp_ind,
00375 &recv_count_arr[i].fp_ind,
00376 6*sizeof(ADIO_Offset));
00377
00378 recv_req_arr_sz++;
00379 }
00380 }
00381 }
00382
00383
00384
00385 send_req_arr = (MPI_Request *) ADIOI_Calloc(2*(send_req_arr_sz)+1,
00386 sizeof(MPI_Request));
00387
00388 j = 0;
00389 if (recv_req_arr_sz > 0) {
00390 assert (fd->is_agg);
00391 recv_req_arr = (MPI_Request *) ADIOI_Calloc(2*(recv_req_arr_sz),
00392 sizeof(MPI_Request));
00393 for (i = 0; i < nprocs; i++) {
00394 if (recv_count_arr[i].count > 0) {
00395 MPI_Irecv(client_file_view_state_arr[i].flat_type_p->indices,
00396 recv_count_arr[i].count, ADIO_OFFSET, i,
00397 INDICES, fd->comm, &recv_req_arr[j]);
00398 j++;
00399 MPI_Irecv(client_file_view_state_arr[i].flat_type_p->blocklens,
00400 recv_count_arr[i].count, MPI_INT, i,
00401 BLOCK_LENS, fd->comm, &recv_req_arr[j]);
00402 j++;
00403 }
00404 }
00405 }
00406
00407 if (fd->hints->cb_alltoall != ADIOI_HINT_DISABLE) {
00408 j = 0;
00409 for (i = 0; i < nprocs; i++) {
00410 if (send_count_arr[i].count > 0) {
00411 MPI_Isend(flat_file_p->indices,
00412 send_count_arr[i].count, ADIO_OFFSET, i,
00413 INDICES, fd->comm, &send_req_arr[j]);
00414 j++;
00415 MPI_Isend(flat_file_p->blocklens,
00416 send_count_arr[i].count, MPI_INT, i,
00417 BLOCK_LENS, fd->comm, &send_req_arr[j]);
00418 j++;
00419 }
00420 }
00421 } else {
00422 j = 0;
00423 for (i = 0; i < fd->hints->cb_nodes; i++) {
00424 if (send_count_arr[i].count > 0) {
00425 MPI_Isend(flat_file_p->indices,
00426 send_count_arr[i].count, ADIO_OFFSET,
00427 fd->hints->ranklist[i], INDICES, fd->comm,
00428 &send_req_arr[j]);
00429 j++;
00430 MPI_Isend(flat_file_p->blocklens,
00431 send_count_arr[i].count, MPI_INT,
00432 fd->hints->ranklist[i], BLOCK_LENS, fd->comm,
00433 &send_req_arr[j]);
00434 j++;
00435 }
00436 }
00437 }
00438
00439
00440
00441 statuses = (MPI_Status *)
00442 ADIOI_Malloc(1 + 2 * ADIOI_MAX(send_req_arr_sz,recv_req_arr_sz)
00443 * sizeof(MPI_Status));
00444
00445 if (send_req_arr_sz > 0) {
00446 MPI_Waitall(2 * send_req_arr_sz, send_req_arr, statuses);
00447 ADIOI_Free(send_count_arr);
00448 ADIOI_Free(send_req_arr);
00449 }
00450 if (recv_req_arr_sz > 0) {
00451 MPI_Waitall(2 * recv_req_arr_sz, recv_req_arr, statuses);
00452 ADIOI_Free(recv_count_arr);
00453 ADIOI_Free(recv_req_arr);
00454 }
00455 ADIOI_Free(statuses);
00456
00457 if (fd->is_agg == 1)
00458 {
00459 ADIOI_init_view_state(file_ptr_type,
00460 nprocs,
00461 client_file_view_state_arr,
00462 TEMP_OFF);
00463 ADIOI_init_view_state(file_ptr_type,
00464 nprocs,
00465 client_file_view_state_arr,
00466 REAL_OFF);
00467 }
00468
00469 #ifdef DEBUG
00470 if (fd->is_agg == 1)
00471 {
00472 ADIOI_Flatlist_node *fr_node_p = CtvAccess(ADIOI_Flatlist);
00473 for (i = 0; i < nprocs; i++)
00474 {
00475 fprintf(stderr, "client_file_view_state_arr[%d]=(fp_ind=%Ld,"
00476 "disp=%Ld,byte_off=%Ld,sz=%Ld,ext=%Ld\n", i,
00477 client_file_view_state_arr[i].fp_ind,
00478 client_file_view_state_arr[i].disp,
00479 client_file_view_state_arr[i].byte_off,
00480 client_file_view_state_arr[i].sz,
00481 client_file_view_state_arr[i].ext);
00482 }
00483
00484 while (fr_node_p->type !=
00485 fd->file_realm_types[fd->my_cb_nodes_index])
00486 fr_node_p = fr_node_p->next;
00487 assert(fr_node_p != NULL);
00488
00489 fprintf(stderr, "my file realm (idx=%d,st_off=%Ld) ",
00490 fd->my_cb_nodes_index,
00491 fd->file_realm_st_offs[fd->my_cb_nodes_index]);
00492 ADIOI_Print_flatlist_node(fr_node_p);
00493 }
00494 #endif
00495
00496 #ifdef DEBUG2
00497 if (fd->is_agg == 1)
00498 {
00499 for (i = 0; i < nprocs; i++)
00500 {
00501 fprintf(stderr, "client_file_view_state_arr[%d]: ", i);
00502 ADIOI_Print_flatlist_node(
00503 client_file_view_state_arr[i].flat_type_p);
00504 }
00505 }
00506 #endif
00507 #ifdef AGGREGATION_PROFILE
00508 MPE_Log_event (5015, 0, NULL);
00509 #endif
00510 }