mpitypes.c 2.15 KB
Newer Older
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
#include <assert.h>
#include <stdlib.h>

#include "mpitypes.h"

#include "configuration.h"
#include "box.h"

// --------------------------------------------- Helper function declarations

// ==========================================================================

void mpitype_box_init(MPI_Datatype *new_type)
{
   box_t dummy;
   int i;

   // BOX_T_* constants defined in box.h
   int blocklengths[] = {BOX_T_N_INT_MEMBERS, BOX_T_N_LONG_MEMBERS};
   MPI_Datatype types[] = {MPI_INT, MPI_LONG};
   MPI_Aint displacements[2];
   MPI_Aint base;

   MPI_Get_address(&dummy, &base);
   MPI_Get_address(&dummy.BOX_T_FIRST_INT_MEMBER, &displacements[0]);
   MPI_Get_address(&dummy.BOX_T_FIRST_LONG_MEMBER, &displacements[1]);

   for(i = 0; i < 2; i++) displacements[i] -= base;

   MPI_Type_create_struct(2, blocklengths, displacements, types, new_type);
   MPI_Type_commit(new_type);
}

void mpitype_box_free(MPI_Datatype *type)
{
   MPI_Type_free(type);
}

void mpitype_conf_init(MPI_Datatype *new_type)
{
   conf_t dummy;
   int i;
   MPI_Datatype box_type;

   mpitype_box_init(&box_type);

   // CONF_T_* constants defined in configuration.h
   int blocklengths[] = {CONF_T_N_INT_MEMBERS, CONF_T_N_BOX_MEMBERS};
   MPI_Datatype types[] = {MPI_INT, box_type};
   MPI_Aint displacements[2];
   MPI_Aint base;

   MPI_Get_address(&dummy, &base);
   MPI_Get_address(&dummy.CONF_T_FIRST_INT_MEMBER, &displacements[0]);
   MPI_Get_address(&dummy.CONF_T_FIRST_BOX_MEMBER, &displacements[1]);

   for(i = 0; i < 2; i++) displacements[i] -= base;

   MPI_Type_create_struct(2, blocklengths, displacements, types, new_type);
   MPI_Type_commit(new_type);

   mpitype_box_free(&box_type);
}

void mpitype_conf_free(MPI_Datatype *type)
{
   MPI_Type_free(type);
}

// --------------------------------------------------------- Helper functions

void mpitype_indexed_int(int count, int *displacements, MPI_Datatype *type)
{
   MPI_Type_create_indexed_block(count, 1, displacements, MPI_INT, type);
   MPI_Type_commit(type);
}

void mpitype_indexed_double(int count, int *displacements, MPI_Datatype *type)
{
   MPI_Type_create_indexed_block(count, 1, displacements, MPI_DOUBLE, type);
   MPI_Type_commit(type);
}