1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/*============================================================================
* Unit test for fvm_file.c;
*============================================================================*/
/*
This file is part of Code_Saturne, a general-purpose CFD tool.
Copyright (C) 1998-2019 EDF S.A.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*----------------------------------------------------------------------------*/
#include "cs_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bft_error.h>
#include <bft_mem.h>
#include <bft_printf.h>
#include "cs_file.h"
/*---------------------------------------------------------------------------*/
static void
_create_test_data(void)
{
int i;
cs_file_t *f;
char header[80];
char footer[80];
int iarray[30];
double farray[30];
#if defined(HAVE_MPI)
int mpi_flag;
MPI_Comm comm = MPI_COMM_NULL;
#endif
sprintf(header, "fvm test file");
for (i = strlen(header); i < 80; i++)
header[i] = '\0';
sprintf(footer, "fvm test file end");
for (i = strlen(footer); i < 80; i++)
footer[i] = '\0';
for (i = 0; i < 30; i++)
iarray[i] = i+1;
for (i = 0; i < 30; i++)
farray[i] = i+1;
#if defined(HAVE_MPI)
MPI_Initialized(&mpi_flag);
if (mpi_flag != 0) {
comm = MPI_COMM_WORLD;
}
f = cs_file_open("file_test_data",
CS_FILE_MODE_WRITE,
CS_FILE_STDIO_SERIAL,
MPI_INFO_NULL,
comm,
comm);
#else
f = cs_file_open("file_test_data",
CS_FILE_MODE_WRITE,
0);
#endif
cs_file_set_big_endian(f);
cs_file_write_global(f, header, 1, 80);
cs_file_write_global(f, iarray, sizeof(int), 30);
cs_file_write_global(f, farray, sizeof(double), 30);
cs_file_write_global(f, footer, 1, 80);
f = cs_file_free(f);
}
/*---------------------------------------------------------------------------*/
int
main (int argc, char *argv[])
{
char mem_trace_name[32];
char output_file_name[32];
char buf[80];
int ibuf[30];
double dbuf[30];
cs_gnum_t i;
int a_id, p_id;
int size = 1;
int rank = 0;
size_t retval = 0;
cs_gnum_t block_start, block_end;
cs_gnum_t block_start_2, block_end_2;
cs_file_off_t off1 = -1, off2 = -1;
cs_file_t *f = NULL;
#if defined(HAVE_MPI_IO)
const int n_pos = 2;
const int n_access = 5;
const cs_file_access_t access[5] = {CS_FILE_STDIO_SERIAL,
CS_FILE_STDIO_PARALLEL,
CS_FILE_MPI_INDEPENDENT,
CS_FILE_MPI_NON_COLLECTIVE,
CS_FILE_MPI_COLLECTIVE};
const cs_file_mpi_positionning_t pos[2] = {CS_FILE_MPI_EXPLICIT_OFFSETS,
CS_FILE_MPI_INDIVIDUAL_POINTERS};
#else
const int n_pos = 1;
const int n_access = 1;
const int access[1] = {CS_FILE_STDIO_SERIAL};
const cs_file_mpi_positionning_t pos[1] = {CS_FILE_MPI_EXPLICIT_OFFSETS};
#endif
#if defined(HAVE_MPI)
MPI_Status status;
int sync = 1;
/* Initialization */
MPI_Init(&argc, &argv);
cs_glob_mpi_comm = MPI_COMM_WORLD;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
block_start = rank * (30./size) + 1;
block_end = (rank + 1) * (30./size) + 1;
if (rank == size - 1)
block_end = 31;
block_start_2 = rank * (15./size) + 1;
block_end_2 = (rank + 1) * (15./size) + 1;
if (rank == size - 1)
block_end_2 = 16;
#else
block_start = 1;
block_end = 31;
block_start_2 = 1;
block_end_2 = 16;
#endif /* (HAVE_MPI) */
if (size > 1)
sprintf(mem_trace_name, "cs_file_test_mem.%d", rank);
else
strcpy(mem_trace_name, "cs_file_test_mem");
bft_mem_init(mem_trace_name);
_create_test_data();
/* Loop on tests */
for (a_id = 0; a_id < n_access; a_id++) {
for (p_id = 0; p_id < n_pos; p_id++) {
if (access[a_id] >= CS_FILE_MPI_INDEPENDENT) {
cs_file_set_mpi_io_positionning(pos[p_id]);
if (rank == 0)
bft_printf("Running test: %d-%d\n"
"-------------\n\n", a_id, p_id);
sprintf(output_file_name, "output_data_%d_%d", a_id+1, p_id+1);
}
else {
if (rank == 0)
bft_printf("Running test: %d\n"
"-------------\n\n", a_id);
sprintf(output_file_name, "output_data_%d", a_id+1);
}
/* Read and seek/set tests */
/*-------------------------*/
#if defined(HAVE_MPI)
f = cs_file_open("file_test_data",
CS_FILE_MODE_READ,
access[a_id],
MPI_INFO_NULL,
MPI_COMM_WORLD,
MPI_COMM_WORLD);
#else
f = cs_file_open("file_test_data",
CS_FILE_MODE_READ,
access[a_id]);
#endif /* (HAVE_MPI) */
cs_file_set_big_endian(f);
cs_file_dump(f);
retval = cs_file_read_global(f, buf, 1, 80);
bft_printf("rank %d, readbuf = %s (returned %d)\n\n",
rank, buf, (int)retval);
for (i = 0; i < 30; i++)
ibuf[i] = 0;
retval = cs_file_read_block(f, ibuf, sizeof(int), 1,
block_start, block_end);
#if defined(HAVE_MPI) /* Serialize dump */
if (rank > 0)
MPI_Recv(&sync, 1, MPI_INT, rank - 1, 9876, MPI_COMM_WORLD, &status);
#endif
bft_printf("\nRead by rank %d (returned %d):\n\n", rank, (int)retval);
for (i = block_start; i < block_end; i++)
bft_printf(" ival[%d] = %d\n", (int)i, (int)(ibuf[i-block_start]));
#if defined(HAVE_MPI)
if (rank < size - 1)
MPI_Send(&sync, 1, MPI_INT, rank + 1, 9876, MPI_COMM_WORLD);
#endif
off1 = cs_file_tell(f);
retval = cs_file_read_block(f, dbuf, sizeof(double), 2,
block_start_2, block_end_2);
off2 = cs_file_tell(f);
#if defined(HAVE_MPI) /* Serialize dump */
if (rank > 0)
MPI_Recv(&sync, 1, MPI_INT, rank - 1, 9876, MPI_COMM_WORLD, &status);
#endif
bft_printf("\nRead by rank %d (returned %d):\n\n", rank, (int)retval);
for (i = block_start_2; i < block_end_2; i++) {
bft_printf(" dval[%d] = %f\n", (int)(i*2 - 1),
(dbuf[(i-block_start_2)*2]));
bft_printf(" dval[%d] = %f\n", (int)(i*2),
(dbuf[(i-block_start_2)*2+1]));
}
#if defined(HAVE_MPI)
if (rank < size - 1)
MPI_Send(&sync, 1, MPI_INT, rank + 1, 9876, MPI_COMM_WORLD);
#endif
#if defined(HAVE_MPI)
MPI_Barrier(MPI_COMM_WORLD);
#endif
bft_printf("barrier passed by rank %d\n", rank);
bft_printf("\nOffsets saved by rank %d: %ld, %ld\n\n",
rank, (long)off1, (long)off2);
retval = cs_file_read_global(f, buf, 1, 80);
bft_printf("rank %d, buf = %s (returned %d)\n", rank, buf, (int)retval);
/* Test seek by re-reading at saved offset */
cs_file_seek(f, off1, CS_FILE_SEEK_SET);
memset(dbuf, 0, (block_end_2 - block_start_2)*sizeof(double));
retval = cs_file_read_block(f, dbuf, sizeof(double), 1,
block_start_2, block_end_2);
#if defined(HAVE_MPI) /* Serialize dump */
if (rank > 0)
MPI_Recv(&sync, 1, MPI_INT, rank - 1, 9876, MPI_COMM_WORLD, &status);
#endif
bft_printf("\nRe-read by rank %d (returned %d):\n\n", rank, (int)retval);
for (i = block_start; i < block_end; i++)
bft_printf(" dval[%d] = %f\n", (int)i, (dbuf[i-block_start]));
#if defined(HAVE_MPI)
if (rank < size - 1)
MPI_Send(&sync, 1, MPI_INT, rank + 1, 9876, MPI_COMM_WORLD);
#endif
cs_file_seek(f, off2, CS_FILE_SEEK_SET);
retval = cs_file_read_global(f, buf, 1, 80);
bft_printf("rank %d, re-read buf = %s (returned %d)\n",
rank, buf, (int)retval);
f = cs_file_free(f);
/* Write tests */
/*-------------*/
#if defined(HAVE_MPI)
f = cs_file_open(output_file_name,
CS_FILE_MODE_WRITE,
access[a_id],
MPI_INFO_NULL,
MPI_COMM_WORLD,
MPI_COMM_WORLD);
#else
f = cs_file_open(output_file_name,
CS_FILE_MODE_WRITE,
access[a_id]);
#endif /* (HAVE_MPI) */
cs_file_set_big_endian(f);
cs_file_dump(f);
sprintf(buf, "fvm test file");
for (i = strlen(buf); i < 80; i++)
buf[i] = '\0';
retval = cs_file_write_global(f, buf, 1, 80);
if (rank == 0)
bft_printf("rank %d, wrote %d global values.\n", rank, (int)retval);
for (i = block_start_2; i < block_end_2; i++) {
ibuf[(i-block_start_2)*2] = i*2 - 1;
ibuf[(i-block_start_2)*2 + 1] = i*2;
}
for (i = block_start; i < block_end; i++)
dbuf[i-block_start] = i;
retval = cs_file_write_block(f, ibuf, sizeof(int), 2,
block_start_2, block_end_2);
bft_printf("rank %d, wrote %d block values.\n", rank, (int)retval);
retval = cs_file_write_block_buffer(f, dbuf, sizeof(double), 1,
block_start, block_end);
bft_printf("rank %d, wrote %d block (buffer) values.\n",
rank, (int)retval);
sprintf(buf, "fvm test file end");
for (i = strlen(buf); i < 80; i++)
buf[i] = '\0';
retval = cs_file_write_global(f, buf, 1, 80);
if (rank == 0)
bft_printf("rank %d, wrote %d global values.\n", rank, (int)retval);
f = cs_file_free(f);
if (access[a_id] < CS_FILE_MPI_INDEPENDENT)
break;
}
}
/* We are finished */
bft_mem_end();
#if defined(HAVE_MPI)
MPI_Finalize();
#endif
exit (EXIT_SUCCESS);
}
|