~ubuntu-branches/ubuntu/trusty/nwchem/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tools/ga-5-2/global/testing/mulmatpatchc.c

  • Committer: Package Import Robot
  • Author(s): Michael Banck, Daniel Leidert, Andreas Tille, Michael Banck
  • Date: 2013-07-04 12:14:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130704121455-5tvsx2qabor3nrui
Tags: 6.3-1
* New upstream release.
* Fixes anisotropic properties (Closes: #696361).
* New features include:
  + Multi-reference coupled cluster (MRCC) approaches
  + Hybrid DFT calculations with short-range HF 
  + New density-functionals including Minnesota (M08, M11) and HSE hybrid
    functionals
  + X-ray absorption spectroscopy (XAS) with TDDFT
  + Analytical gradients for the COSMO solvation model
  + Transition densities from TDDFT 
  + DFT+U and Electron-Transfer (ET) methods for plane wave calculations
  + Exploitation of space group symmetry in plane wave geometry optimizations
  + Local density of states (LDOS) collective variable added to Metadynamics
  + Various new XC functionals added for plane wave calculations, including
    hybrid and range-corrected ones
  + Electric field gradients with relativistic corrections 
  + Nudged Elastic Band optimization method
  + Updated basis sets and ECPs 

[ Daniel Leidert ]
* debian/watch: Fixed.

[ Andreas Tille ]
* debian/upstream: References

[ Michael Banck ]
* debian/upstream (Name): New field.
* debian/patches/02_makefile_flags.patch: Refreshed.
* debian/patches/06_statfs_kfreebsd.patch: Likewise.
* debian/patches/07_ga_target_force_linux.patch: Likewise.
* debian/patches/05_avoid_inline_assembler.patch: Removed, no longer needed.
* debian/patches/09_backported_6.1.1_fixes.patch: Likewise.
* debian/control (Build-Depends): Added gfortran-4.7 and gcc-4.7.
* debian/patches/10_force_gcc-4.7.patch: New patch, explicitly sets
  gfortran-4.7 and gcc-4.7, fixes test suite hang with gcc-4.8 (Closes:
  #701328, #713262).
* debian/testsuite: Added tests for COSMO analytical gradients and MRCC.
* debian/rules (MRCC_METHODS): New variable, required to enable MRCC methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if HAVE_CONFIG_H
 
2
#   include "config.h"
 
3
#endif
 
4
 
 
5
#include <assert.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <time.h>
 
9
 
 
10
#include "ga.h"
 
11
#include "macdecls.h"
 
12
#include "mp3.h"
 
13
#include "xgemm.h"
 
14
 
 
15
#if defined(FUJITSU) || defined(CRAY_YMP)
 
16
#   define THRESH 1.0e-10
 
17
#else
 
18
#   define THRESH 1.0e-20
 
19
#endif
 
20
#define ABS(x) ((x) >= 0.0 ? (x) : -(x))
 
21
#define MAX(x,y) ((x) >= (y) ? (x) : (y))
 
22
#define MISMATCH(x,y) (ABS((x)-(y)) / MAX(1.0,ABS((x)))) > THRESH
 
23
#define NMAX 100
 
24
#define POW2(x) ((x)*(x))
 
25
#define POW4(x) ((x)*(x)*(x)*(x))
 
26
#define SPECIFIC_CASE 0
 
27
#define VERBOSE 0
 
28
 
 
29
static void dpatch_test(
 
30
        int size,
 
31
        int dist_same,
 
32
        int ampos, int akpos,
 
33
        int bkpos, int bnpos,
 
34
        int cmpos, int cnpos);
 
35
static void dpatch_test2();
 
36
 
 
37
int main(int argc, char **argv)
 
38
{
 
39
    int bufsize, gasize;
 
40
    time_t t;
 
41
    long seed;
 
42
    int sizes[] = {10, 50, 100};
 
43
    int s, same_dist, ampos, akpos, bkpos, bnpos, cmpos, cnpos;
 
44
 
 
45
    MP_INIT(argc,argv);
 
46
    GA_Initialize_args(&argc,&argv);
 
47
 
 
48
    if (0 == GA_Nodeid()) {
 
49
        printf("  GA initialized\n");
 
50
        fflush(stdout);
 
51
    }
 
52
 
 
53
    /* we need srandom seed to be the same on all procs */
 
54
    t = time(NULL);
 
55
    seed = (long)t;
 
56
    GA_Lgop(&seed, 1, "max");
 
57
    srandom(seed);
 
58
#if VERBOSE
 
59
    printf("seed=%ld\n", seed);
 
60
    fflush(stdout);
 
61
#endif
 
62
    GA_Sync();
 
63
 
 
64
    /* we want to force distribution of innermost loop in nga_mulmat_patch
 
65
     by providing less buffer memory than needed */
 
66
 
 
67
    if(GA_Uses_ma()) {
 
68
        gasize = (POW4(NMAX) * 3)/GA_Nnodes();
 
69
    }
 
70
    else {
 
71
        gasize = 0;
 
72
    }
 
73
 
 
74
    bufsize = (NMAX/2 + 1)*(NMAX/3 + 1)*2 + POW2(NMAX/2 + 1);
 
75
    bufsize = bufsize*6/7;
 
76
 
 
77
    if (!MA_init(MT_DBL, 10, gasize+bufsize+500000)) {
 
78
        GA_Error("MA_init failed", -1);
 
79
    }
 
80
    if (0 == GA_Nodeid()) {
 
81
        printf(" \n");
 
82
        printf(" CHECKING MATRIX MULTIPLICATION FOR PATCHES \n");
 
83
#if VERBOSE
 
84
        printf("gasize and bufsize are %d %d\n", gasize, bufsize);
 
85
#endif
 
86
        printf(" \n");
 
87
        fflush(stdout);
 
88
    }
 
89
#if SPECIFIC_CASE
 
90
    dpatch_test(10, 0, 0, 1, 1, 2, 2, 3);
 
91
#else
 
92
    for (s=0; s<1; ++s) {
 
93
        for (same_dist=0; same_dist<2; ++same_dist) {
 
94
            for (ampos=0; ampos<4; ++ampos) {
 
95
                for (akpos=ampos+1; akpos<4; ++akpos) {
 
96
                    for (bkpos=0; bkpos<4; ++bkpos) {
 
97
                        for (bnpos=bkpos+1; bnpos<4; ++bnpos) {
 
98
                            for (cmpos=0; cmpos<4; ++cmpos) {
 
99
                                for (cnpos=cmpos+1; cnpos<4; ++cnpos) {
 
100
                                    dpatch_test(sizes[s],
 
101
                                            same_dist,
 
102
                                            ampos, akpos,
 
103
                                            bkpos, bnpos,
 
104
                                            cmpos, cnpos);
 
105
                                }
 
106
                            }
 
107
                        }
 
108
                    }
 
109
                }
 
110
            }
 
111
        }
 
112
    }
 
113
#endif
 
114
#if 0
 
115
    dpatch_test2();
 
116
#endif
 
117
 
 
118
    if(0 == GA_Nodeid()) {
 
119
        printf(" All tests successful \n");
 
120
        fflush(stdout);
 
121
    }
 
122
 
 
123
    GA_Terminate();
 
124
    MP_FINALIZE();
 
125
    return 0;
 
126
}
 
127
 
 
128
 
 
129
/**
 
130
 * We start with a 4-dimensional array and multiply various 2D patches,
 
131
 * comparing results against a locally computed dgemm.
 
132
 */
 
133
static void dpatch_test(
 
134
        int size,
 
135
        int dist_same,
 
136
        int ampos, int akpos,
 
137
        int bkpos, int bnpos,
 
138
        int cmpos, int cnpos)
 
139
{
 
140
    const int ndim=4;
 
141
    double *a, *b, *c, *r, *v;
 
142
    double alpha, beta;
 
143
    int nproc, me;
 
144
    int i, j;
 
145
    int dims[4], chunk[4], rld[3];
 
146
    int alo[4], ahi[4], ald[3];
 
147
    int blo[4], bhi[4], bld[3];
 
148
    int clo[4], chi[4], cld[3];
 
149
    int g_a, g_b, g_c;
 
150
    char ta, tb;
 
151
    int m, n, k;
 
152
 
 
153
    me = GA_Nodeid();
 
154
    nproc = GA_Nnodes();
 
155
 
 
156
    assert(size <= NMAX);
 
157
 
 
158
#if SPECIFIC_CASE
 
159
    m = 1;
 
160
    n = 1;
 
161
    k = 4;
 
162
#else
 
163
    m = random() % (size/2);
 
164
    n = random() % (size/2);
 
165
    k = random() % (size/2);
 
166
    if (m<=0) m = 1;
 
167
    if (n<=0) n = 1;
 
168
    if (k<=0) k = 1;
 
169
#endif
 
170
    if (0 == me) {
 
171
        printf("size=%d, dist_same=%d ampos=%d akpos=%d bkpos=%d bnpos=%d cmpos=%d cnpos=%d m=%d n=%d k=%d\n", size, dist_same, ampos, akpos, bkpos, bnpos, cmpos, cnpos, m, n, k);
 
172
        fflush(stdout);
 
173
    }
 
174
 
 
175
    a = malloc(sizeof(double)*size*size);
 
176
    b = malloc(sizeof(double)*size*size);
 
177
    c = malloc(sizeof(double)*size*size);
 
178
    r = malloc(sizeof(double)*size*size);
 
179
    memset(a, 0, sizeof(double)*size*size);
 
180
    memset(b, 0, sizeof(double)*size*size);
 
181
    memset(c, 0, sizeof(double)*size*size);
 
182
    memset(r, 0, sizeof(double)*size*size);
 
183
 
 
184
    /* establish the shape and default chunking of the global arrays */
 
185
    for (i=0; i<ndim; ++i) {
 
186
        alo[i] = 0;
 
187
        ahi[i] = size-1;
 
188
        dims[i] = size;
 
189
        chunk[i] = -1;
 
190
    }
 
191
 
 
192
    /* create g_a */
 
193
    g_a = NGA_Create(C_DBL, ndim, dims, "a", chunk);
 
194
    if (0 == g_a) {
 
195
        printf("NGA_Create failed\n");
 
196
        fflush(stdout);
 
197
        GA_Error("... exiting", 1);
 
198
    }
 
199
 
 
200
    /* create g_b and g_c */
 
201
    if (dist_same) {
 
202
        g_b = GA_Duplicate(g_a, "a_duplicated");
 
203
        if(1 == GA_Compare_distr(g_a, g_b)) {
 
204
            GA_Error("g_b distribution different",1);
 
205
        }
 
206
        g_c = GA_Duplicate(g_a, "a_duplicated_again");
 
207
        if(1 == GA_Compare_distr(g_a, g_c)) {
 
208
            GA_Error("g_c distribution different",1);
 
209
        }
 
210
    }
 
211
    else { 
 
212
        chunk[ndim-1] = size;
 
213
        g_b = NGA_Create(C_DBL, ndim, dims, "b1", chunk);
 
214
        if (0 == g_b) {
 
215
            GA_Error("NGA_Create failed:b1",1);
 
216
        }
 
217
        chunk[ndim-1] = 0;
 
218
        chunk[ndim-2] = size;
 
219
        g_c = NGA_Create(C_DBL, ndim, dims, "c1", chunk);
 
220
        if (0 == g_c) {
 
221
            GA_Error("NGA_Create failed:c1",1);
 
222
        }
 
223
    }
 
224
 
 
225
#if 0
 
226
    if (0 == me) {
 
227
        printf("\n");
 
228
        printf("> Checking NGA_Matmul_patch ... \n");
 
229
        fflush(stdout);
 
230
    }
 
231
#endif
 
232
 
 
233
    /* fill the g_a and g_b global arrays entirely with data */
 
234
    for (i=0; i<ndim; ++i) {
 
235
        alo[i] = 0;
 
236
        ahi[i] = size-1;
 
237
        blo[i] = 0;
 
238
        bhi[i] = size-1;
 
239
        if (i<ndim-1) {
 
240
            ald[i] = size;
 
241
            bld[i] = size;
 
242
        }
 
243
    }
 
244
    if (0 == me) {
 
245
        /* generate some local data (an enumerated range) */
 
246
        double *v = malloc(sizeof(double)*POW4(size));
 
247
        memset(v, 0, sizeof(double)*POW4(size));
 
248
        for (i=0; i<POW4(size); ++i) {
 
249
            v[i] = i;
 
250
        }
 
251
        NGA_Put(g_a,alo,ahi,v,ald);
 
252
        NGA_Put(g_b,blo,bhi,v,bld);
 
253
        free(v);
 
254
    }
 
255
    GA_Zero(g_c);
 
256
    GA_Sync();
 
257
 
 
258
    /* for g_a, g_b, g_c generate a random starting index for patches */
 
259
    for (i=0; i<ndim; ++i) {
 
260
        ahi[i] = alo[i] = random() % (size/2);
 
261
        bhi[i] = blo[i] = random() % (size/2);
 
262
        chi[i] = clo[i] = random() % (size/2);
 
263
        if (i<ndim-1) {
 
264
            ald[i] = 1;
 
265
            bld[i] = 1;
 
266
            cld[i] = 1;
 
267
        }
 
268
    }
 
269
    ahi[ampos] += m - 1;
 
270
    ahi[akpos] += k - 1;
 
271
    if (ampos>0) ald[ampos-1] = m;
 
272
    if (akpos>0) ald[akpos-1] = k;
 
273
    bhi[bkpos] += k - 1;
 
274
    bhi[bnpos] += n - 1;
 
275
    if (bkpos>0) bld[bkpos-1] = k;
 
276
    if (bnpos>0) bld[bnpos-1] = n;
 
277
    chi[cmpos] +=  m - 1;
 
278
    chi[cnpos] +=  n - 1;
 
279
    if (cmpos>0) cld[cmpos-1] = m;
 
280
    if (cnpos>0) cld[cnpos-1] = n;
 
281
 
 
282
#if 0
 
283
    printf("a[%d:%d,%d:%d,%d:%d,%d:%d] %dx%dx%dx%d (%dx%d)\n",
 
284
            alo[0], ahi[0],
 
285
            alo[1], ahi[1],
 
286
            alo[2], ahi[2],
 
287
            alo[3], ahi[3],
 
288
            m, 1, k, 1, m, k);
 
289
    printf("ald={%d,%d,%d}\n", ald[0], ald[1], ald[2]);
 
290
    printf("b[%d:%d,%d:%d,%d:%d,%d:%d] %dx%dx%dx%d (%dx%d)\n",
 
291
            blo[0], bhi[0],
 
292
            blo[1], bhi[1],
 
293
            blo[2], bhi[2],
 
294
            blo[3], bhi[3],
 
295
            1, k, 1, n, k, n);
 
296
    printf("bld={%d,%d,%d}\n", bld[0], bld[1], bld[2]);
 
297
    printf("c[%d:%d,%d:%d,%d:%d,%d:%d] %dx%dx%dx%d (%dx%d)\n",
 
298
            clo[0], chi[0],
 
299
            clo[1], chi[1],
 
300
            clo[2], chi[2],
 
301
            clo[3], chi[3],
 
302
            m, 1, 1, n, m, n);
 
303
    printf("cld={%d,%d,%d}\n", cld[0], cld[1], cld[2]);
 
304
#endif
 
305
 
 
306
    /* reset our buffers, just in case */
 
307
    memset(a, 0, sizeof(double)*size*size);
 
308
    memset(b, 0, sizeof(double)*size*size);
 
309
    memset(c, 0, sizeof(double)*size*size);
 
310
    memset(r, 0, sizeof(double)*size*size);
 
311
 
 
312
    /* get patches locally and compute locally */
 
313
    NGA_Get(g_a, alo, ahi, a, ald);
 
314
    NGA_Get(g_b, blo, bhi, b, bld);
 
315
    GA_Sync();
 
316
    ta = 'n';
 
317
    tb = 'n';
 
318
    alpha = 1e0;
 
319
    beta = 0e0;
 
320
    xb_dgemm(&tb, &ta, &n, &m, &k, &alpha, b, &n, a, &k, &beta, c, &n);
 
321
 
 
322
    /* perform global computation */
 
323
    NGA_Matmul_patch(ta, tb, &alpha, &beta, 
 
324
            g_a, alo, ahi,
 
325
            g_b, blo, bhi,
 
326
            g_c, clo, chi);
 
327
    GA_Sync();
 
328
 
 
329
    /* get global result into local buf and compare results */
 
330
    NGA_Get(g_c, clo, chi, r, cld);
 
331
    GA_Sync();
 
332
    for (i=0; i<1; ++i) {
 
333
        if (MISMATCH(c[i],r[i])) {
 
334
            printf("at %d %f != %f\n", i, c[i], r[i]);
 
335
            GA_Error("mismatch", 1);
 
336
        }
 
337
    }
 
338
 
 
339
    free(a);
 
340
    free(b);
 
341
    free(c);
 
342
    free(r);
 
343
    GA_Destroy(g_a);
 
344
    GA_Destroy(g_b);
 
345
    GA_Destroy(g_c);
 
346
}
 
347
 
 
348
 
 
349
static void dpatch_test2()
 
350
{
 
351
}