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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "moab/Core.hpp"
#include "moab/ParallelComm.hpp"
#include "MBTagConventions.hpp"
#include "moab_mpi.h"
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cstring>
#include <cmath>
#include <cassert>
#include <cstdio>
#include <sstream>

using namespace moab;

#define TPRINT( A ) tprint( ( A ) )
static void tprint( const char* A )
{
    int rank;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    char buffer[128];
    sprintf( buffer, "%02d: %6.2f: %s\n", rank, (double)clock() / CLOCKS_PER_SEC, A );
    fputs( buffer, stderr );
}

const int DEFAULT_INTERVALS   = 2;
const char* DEFAULT_FILE_NAME = "parallel_write_test.h5m";

// Create mesh for each processor that is a cube of hexes
// with the specified interval count along each edge.  Cubes
// of mesh will be positioned and vertex IDs assigned such that
// there are shared entities.  If the cubic root of the number
// of processors is a whole number, then each processors mesh
// will be a cube in a grid with that number of processor blocks
// along each edge.  Otherwise processor blocks will be arranged
// within the subset of the grid that is the ceiling of the cubic
// root of the comm size such that there are no disjoint regions.
ErrorCode generate_mesh( Interface& moab, int intervals );

const char args[] = "[-i <intervals>] [-o <filename>] [-L <filename>] [-g <n>]";
void help()
{
    std::cout << "parallel_write_test " << args << std::endl
              << "  -i <N>    Each processor owns an NxNxN cube of hex elements (default: " << DEFAULT_INTERVALS << ")"
              << std::endl
              << "  -o <name> Retain output file and name it as specified." << std::endl
              << "  -L <name> Write local mesh to file name prefixed with MPI rank" << std::endl
              << "  -g <n>    Specify writer debug output level" << std::endl
              << "  -R        Skip resolve of shared entities (interface ents will be duplicated in file)" << std::endl
              << std::endl
              << "This program creates a (non-strict) subset of a regular hex mesh "
                 "such that the mesh is already partitioned, and then attempts to "
                 "write that mesh using MOAB's parallel HDF5 writer.  The mesh size "
                 "will scale with the number of processors and the number of elements "
                 "per processor (the latter is a function of the value specified "
                 "with the '-i' flag.)"
              << std::endl
              << std::endl
              << "Let N = ceil(cbrt(P)), where P is the number of processes.  "
                 "The mesh will be some subset of a cube with one corner at the "
                 "origin and the other at (N,N,N).  Each processor will own a "
                 "non-overlapping 1x1x1 unit block of mesh within that cube.  "
                 "If P is a power of 3, then the entire NxNxN cube will be "
                 "filled with hex elements.  Otherwise, some connected subset "
                 "of the cube will be meshed.  Each processor is assigned a "
                 "sub-block of the cube by rank where the blocks are enumerated "
                 "sequentally with x increasing most rapidly and z least rapidly."
              << std::endl
              << std::endl
              << "The size of the mesh owned by each processor is controlled by "
                 "the number of intervals along each edge of its block of mesh.  "
                 "If each block has N intervals, than each processor will have "
                 "N^3 hex elements."
              << std::endl
              << std::endl;
}

int main( int argc, char* argv[] )
{
    int ierr = MPI_Init( &argc, &argv );
    if( ierr )
    {
        std::cerr << "MPI_Init failed with error code: " << ierr << std::endl;
        return ierr;
    }
    int rank;
    ierr = MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    if( ierr )
    {
        std::cerr << "MPI_Comm_rank failed with error code: " << ierr << std::endl;
        return ierr;
    }
    int size;
    ierr = MPI_Comm_size( MPI_COMM_WORLD, &size );
    if( ierr )
    {
        std::cerr << "MPI_Comm_size failed with error code: " << ierr << std::endl;
        return ierr;
    }

    // settings controlled by CL flags
    const char* output_file_name = 0;
    const char* indiv_file_name  = 0;
    int intervals = 0, debug_level = 0;
    // state for CL flag processing
    bool expect_intervals    = false;
    bool expect_file_name    = false;
    bool expect_indiv_file   = false;
    bool skip_resolve_shared = false;
    bool expect_debug_level  = false;
    // process CL args
    for( int i = 1; i < argc; ++i )
    {
        if( expect_intervals )
        {
            char* endptr = 0;
            intervals    = (int)strtol( argv[i], &endptr, 0 );
            if( *endptr || intervals < 1 )
            {
                std::cerr << "Invalid block interval value: " << argv[i] << std::endl;
                return 1;
            }
            expect_intervals = false;
        }
        else if( expect_indiv_file )
        {
            indiv_file_name   = argv[i];
            expect_indiv_file = false;
        }
        else if( expect_file_name )
        {
            output_file_name = argv[i];
            expect_file_name = false;
        }
        else if( expect_debug_level )
        {
            debug_level = atoi( argv[i] );
            if( debug_level < 1 )
            {
                std::cerr << "Invalid argument following -g flag: \"" << argv[i] << '"' << std::endl;
                return 1;
            }
            expect_debug_level = false;
        }
        else if( !strcmp( "-i", argv[i] ) )
            expect_intervals = true;
        else if( !strcmp( "-o", argv[i] ) )
            expect_file_name = true;
        else if( !strcmp( "-L", argv[i] ) )
            expect_indiv_file = true;
        else if( !strcmp( "-R", argv[i] ) )
            skip_resolve_shared = true;
        else if( !strcmp( "-g", argv[i] ) )
            expect_debug_level = true;
        else if( !strcmp( "-h", argv[i] ) )
        {
            help();
            return 0;
        }
        else
        {
            std::cerr << "Unexpected argument: " << argv[i] << std::endl
                      << "Usage: " << argv[0] << " " << args << std::endl
                      << "       " << argv[0] << " -h" << std::endl
                      << " Try '-h' for help." << std::endl;
            return 1;
        }
    }
    // Check for missing argument after last CL flag
    if( expect_file_name || expect_intervals || expect_indiv_file )
    {
        std::cerr << "Missing argument for '" << argv[argc - 1] << "'" << std::endl;
        return 1;
    }
    // If intervals weren't specified, use default
    if( intervals == 0 )
    {
        std::cout << "Using default interval count: " << DEFAULT_INTERVALS << std::endl;
        intervals = DEFAULT_INTERVALS;
    }
    // If no output file was specified, use default one and note that
    // we need to delete it when the test completes.
    bool keep_output_file = true;
    if( !output_file_name )
    {
        output_file_name = DEFAULT_FILE_NAME;
        keep_output_file = false;
    }

    // Create mesh
    TPRINT( "Generating mesh" );
    double gen_time = MPI_Wtime();
    Core mb;
    Interface& moab = mb;
    ErrorCode rval  = generate_mesh( moab, intervals );
    if( MB_SUCCESS != rval )
    {
        std::cerr << "Mesh creation failed with error code: " << rval << std::endl;
        return (int)rval;
    }
    gen_time = MPI_Wtime() - gen_time;

    // Write out local mesh on each processor if requested.
    if( indiv_file_name )
    {
        TPRINT( "Writing individual file" );
        char buffer[64];
        int width = (int)ceil( log10( size ) );
        sprintf( buffer, "%0*d-", width, rank );
        std::string name( buffer );
        name += indiv_file_name;
        rval = moab.write_file( name.c_str() );
        if( MB_SUCCESS != rval )
        {
            std::cerr << "Failed to write file: " << name << std::endl;
            return (int)rval;
        }
    }

    double res_time = MPI_Wtime();
    Range hexes;
    moab.get_entities_by_type( 0, MBHEX, hexes );
    if( !skip_resolve_shared )
    {
        TPRINT( "Resolving shared entities" );
        // Negotiate shared entities using vertex global IDs
        ParallelComm* pcomm = new ParallelComm( &moab, MPI_COMM_WORLD );
        rval                = pcomm->resolve_shared_ents( 0, hexes, 3, 0 );
        if( MB_SUCCESS != rval )
        {
            std::cerr << "ParallelComm::resolve_shared_ents failed" << std::endl;
            return rval;
        }
    }
    res_time = MPI_Wtime() - res_time;

    TPRINT( "Beginning parallel write" );
    double write_time = MPI_Wtime();
    // Do parallel write
    clock_t t = clock();
    std::ostringstream opts;
    opts << "PARALLEL=WRITE_PART";
    if( debug_level > 0 ) opts << ";DEBUG_IO=" << debug_level;
    rval = moab.write_file( output_file_name, "MOAB", opts.str().c_str() );
    t    = clock() - t;
    if( MB_SUCCESS != rval )
    {
        std::string msg;
        moab.get_last_error( msg );
        std::cerr << "File creation failed with error code: " << moab.get_error_string( rval ) << std::endl;
        std::cerr << "\t\"" << msg << '"' << std::endl;
        return (int)rval;
    }
    write_time = MPI_Wtime() - write_time;

    double times[3] = { gen_time, res_time, write_time };
    double max[3]   = { 0, 0, 0 };
    MPI_Reduce( times, max, 3, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD );

    // Clean up and summarize
    if( 0 == rank )
    {
        double sec = (double)t / CLOCKS_PER_SEC;
        std::cout << "Wrote " << hexes.size() * size << " hexes in " << sec << " seconds." << std::endl;

        if( !keep_output_file )
        {
            TPRINT( "Removing written file" );
            remove( output_file_name );
        }

        std::cout << "Wall time: generate: " << max[0] << ", resovle shared: " << max[1] << ", write_file: " << max[2]
                  << std::endl;
    }

    TPRINT( "Finalizing MPI" );
    return MPI_Finalize();
}

#define IDX( i, j, k ) ( ( num_interval + 1 ) * ( ( num_interval + 1 ) * ( k ) + ( j ) ) + ( i ) )

ErrorCode generate_mesh( Interface& moab, int num_interval )
{
    int rank, size;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    ErrorCode rval;
    Tag global_id = moab.globalId_tag();

    // Each processor will own one cube of mesh within
    // an 3D grid of cubes.  Calculate the dimensions of
    // that grid in numbers of cubes.
    int root = 1;
    while( root * root * root < size )
        ++root;
    int num_x_blocks = root;
    int num_y_blocks = root - 1;
    int num_z_blocks = root - 1;
    if( num_x_blocks * num_y_blocks * num_z_blocks < size ) ++num_y_blocks;
    if( num_x_blocks * num_y_blocks * num_z_blocks < size ) ++num_z_blocks;<--- Variable 'num_z_blocks' is assigned a value that is never used.

    // calculate position of this processor in grid
    int my_z_block = rank / ( num_x_blocks * num_y_blocks );
    int rem        = rank % ( num_x_blocks * num_y_blocks );
    int my_y_block = rem / num_x_blocks;
    int my_x_block = rem % num_x_blocks;

    // Each processor's cube of mesh will be num_iterval^3 elements
    // and will be 1.0 units on a side

    // create vertices
    const int num_x_vtx = num_interval * num_x_blocks + 1;
    const int num_y_vtx = num_interval * num_y_blocks + 1;
    const int x_offset  = my_x_block * num_interval;
    const int y_offset  = my_y_block * num_interval;
    const int z_offset  = my_z_block * num_interval;
    double step         = 1.0 / num_interval;
    std::vector< EntityHandle > vertices( ( num_interval + 1 ) * ( num_interval + 1 ) * ( num_interval + 1 ) );
    std::vector< EntityHandle >::iterator v = vertices.begin();
    for( int k = 0; k <= num_interval; ++k )
    {
        for( int j = 0; j <= num_interval; ++j )
        {
            for( int i = 0; i <= num_interval; ++i )
            {
                double coords[] = { my_x_block + i * step, my_y_block + j * step, my_z_block + k * step };
                EntityHandle h;
                rval = moab.create_vertex( coords, h );
                if( MB_SUCCESS != rval ) return rval;

                int id = 1 + x_offset + i + ( y_offset + j ) * num_x_vtx + ( z_offset + k ) * num_x_vtx * num_y_vtx;
                rval   = moab.tag_set_data( global_id, &h, 1, &id );
                if( MB_SUCCESS != rval ) return rval;

                assert( v != vertices.end() );
                *v++ = h;
            }
        }
    }

    // create hexes
    for( int k = 0; k < num_interval; ++k )
    {
        for( int j = 0; j < num_interval; ++j )
        {
            for( int i = 0; i < num_interval; ++i )
            {
                assert( IDX( i + 1, j + 1, k + 1 ) < (int)vertices.size() );
                const EntityHandle conn[] = { vertices[IDX( i, j, k )],
                                              vertices[IDX( i + 1, j, k )],
                                              vertices[IDX( i + 1, j + 1, k )],
                                              vertices[IDX( i, j + 1, k )],
                                              vertices[IDX( i, j, k + 1 )],
                                              vertices[IDX( i + 1, j, k + 1 )],
                                              vertices[IDX( i + 1, j + 1, k + 1 )],
                                              vertices[IDX( i, j + 1, k + 1 )] };
                EntityHandle elem;
                rval = moab.create_element( MBHEX, conn, 8, elem );
                if( MB_SUCCESS != rval ) return rval;
            }
        }
    }
    /*
      // create interface quads
    for (int j = 0; j < num_interval; ++j) {
      for (int i = 0; i < num_interval; ++i) {
        EntityHandle h;

        const EntityHandle conn1[] = { vertices[IDX(i,  j,  0)],
                                         vertices[IDX(i+1,j,  0)],
                                         vertices[IDX(i+1,j+1,0)],
                                         vertices[IDX(i,  j+1,0)] };
        rval = moab.create_element( MBQUAD, conn1, 4, h );
        if (MB_SUCCESS != rval)
          return rval;

        const EntityHandle conn2[] = { vertices[IDX(i,  j,  num_interval)],
                                         vertices[IDX(i+1,j,  num_interval)],
                                         vertices[IDX(i+1,j+1,num_interval)],
                                         vertices[IDX(i,  j+1,num_interval)] };
        rval = moab.create_element( MBQUAD, conn2, 4, h );
        if (MB_SUCCESS != rval)
          return rval;
      }
    }
    for (int k = 0; k < num_interval; ++k) {
      for (int i = 0; i < num_interval; ++i) {
        EntityHandle h;

        const EntityHandle conn1[] = { vertices[IDX(i,  0,k  )],
                                         vertices[IDX(i+1,0,k  )],
                                         vertices[IDX(i+1,0,k+1)],
                                         vertices[IDX(i,  0,k+1)] };
        rval = moab.create_element( MBQUAD, conn1, 4, h );
        if (MB_SUCCESS != rval)
          return rval;

        const EntityHandle conn2[] = { vertices[IDX(i,  num_interval,k  )],
                                         vertices[IDX(i+1,num_interval,k  )],
                                         vertices[IDX(i+1,num_interval,k+1)],
                                         vertices[IDX(i,  num_interval,k+1)] };
        rval = moab.create_element( MBQUAD, conn2, 4, h );
        if (MB_SUCCESS != rval)
          return rval;
      }
    }
    for (int k = 0; k < num_interval; ++k) {
      for (int j = 0; j < num_interval; ++j) {
        EntityHandle h;

        const EntityHandle conn1[] = { vertices[IDX(0,j,  k  )],
                                         vertices[IDX(0,j+1,k  )],
                                         vertices[IDX(0,j+1,k+1)],
                                         vertices[IDX(0,j,  k+1)] };
        rval = moab.create_element( MBQUAD, conn1, 4, h );
        if (MB_SUCCESS != rval)
          return rval;

        const EntityHandle conn2[] = { vertices[IDX(num_interval,j,  k  )],
                                         vertices[IDX(num_interval,j+1,k  )],
                                         vertices[IDX(num_interval,j+1,k+1)],
                                         vertices[IDX(num_interval,j,  k+1)] };
        rval = moab.create_element( MBQUAD, conn2, 4, h );
        if (MB_SUCCESS != rval)
          return rval;
      }
    }
    */
    return MB_SUCCESS;
}