-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarraytest.c
152 lines (117 loc) · 3.64 KB
/
garraytest.c
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
/* gasp -- global address space toolbox
global arrays tests
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "gasp.h"
typedef struct aelem_tag {
int64_t idx;
int64_t rank;
} aelem_t;
gasp_t *g;
int rank, nranks;
void check_distrib_access_get(garray_t *ga);
void check_put_get(garray_t *ga);
int main(int argc, char **argv)
{
/* initialize gasp */
gasp_init(argc, argv, &g);
rank = gasp_rank();
nranks = gasp_nranks();
if (rank == 0)
printf("gasptest -- %d ranks\n", nranks);
/* even distribution global array tests */
int64_t nelems = nranks * 5;
garray_t *ga;
garray_create(g, nelems, sizeof (aelem_t), NULL, &ga);
if (rank == 0)
printf("[%d] even distribution global array tests\n", rank);
check_distrib_access_get(ga);
check_put_get(ga);
garray_destroy(ga);
/* uneven distribution global array tests */
nelems = nelems + (nranks / 2);
garray_create(g, nelems, sizeof (aelem_t), NULL, &ga);
if (rank == 0)
printf("[%d] uneven distribution global array tests\n", rank);
check_distrib_access_get(ga);
check_put_get(ga);
garray_destroy(ga);
gasp_shutdown(g);
return 0;
}
void check_distrib_access_get(garray_t *ga)
{
/* get the local part of the global array and write into it */
int64_t lo, hi;
garray_distribution(ga, rank, &lo, &hi);
int64_t nlocal_elems = hi - lo + 1;
aelem_t *aptr;
garray_access(ga, lo, hi, (void **)&aptr);
for (int64_t i = 0; i < nlocal_elems; ++i) {
aptr[i].idx = (i + 1) * rank;
aptr[i].rank = rank;
}
garray_flush(ga);
/* wait for all ranks to complete */
gasp_sync();
/* get the whole array on rank 0 and check it */
if (rank == 0) {
int64_t nelems = garray_length(ga);
aelem_t *arr = (aelem_t *)malloc(nelems * sizeof (aelem_t));
int64_t flo, fhi;
flo = 0;
fhi = nelems - 1;
garray_get(ga, flo, fhi, arr);
ldiv_t res = ldiv(nelems, nranks);
int passed = 1;
for (int64_t n = 0; n < nranks; ++n) {
int64_t lidx = (n * res.quot) + (n < res.rem ? n : res.rem);
if (arr[lidx].rank != n) {
printf("array[%" PRId64 "] != %" PRId64 "\n", lidx, n);
passed = 0;
}
}
printf("distribution/access/get tests %s\n", passed ? "passed" : "failed");
free(arr);
}
gasp_sync();
}
void check_put_get(garray_t *ga)
{
int64_t nelems = garray_length(ga);
int64_t lo, hi;
garray_distribution(ga, rank, &lo, &hi);
/* test put by writing to the next rank's first element */
aelem_t tae;
tae.idx = 100 + rank;
tae.rank = rank;
int64_t ti;
ti = (hi + 1) % nelems;
garray_put(ga, ti, ti, &tae);
/* wait for all ranks to complete */
gasp_sync();
/* get the whole array on rank 0 and check it */
if (rank == 0) {
aelem_t *arr = (aelem_t *)malloc(nelems * sizeof (aelem_t));
lo = 0;
hi = nelems - 1;
garray_get(ga, lo, hi, arr);
ldiv_t res = ldiv(nelems, nranks);
int passed = 1;
int64_t srcn = nranks - 1;
for (int64_t n = 0; n < nranks; ++n) {
int64_t lidx = (n * res.quot) + (n < res.rem ? n : res.rem);
if (arr[lidx].idx != 100 + srcn) {
printf("array[%" PRId64 "].idx != %" PRId64 "]\n", lidx, 100 + srcn);
passed = 0;
}
srcn = (srcn + 1) % nranks;
}
printf("put/get tests %s\n", passed ? "passed" : "failed");
free(arr);
}
gasp_sync();
}