~ubuntu-branches/ubuntu/utopic/nwchem/utopic

« back to all changes in this revision

Viewing changes to src/tools/ga-5-1/python/tutorial/srumma.py

  • 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
 
"""A way over-simplified SRUMMA matrix multiplication implementation.
2
 
 
3
 
Assumes square matrices with the shape as a multiple of the block size.
4
 
 
5
 
"""
6
 
import mpi4py.MPI
7
 
import ga
8
 
import numpy as np
9
 
 
10
 
CHUNK_SIZE = 256
11
 
MULTIPLIER = 3
12
 
N = CHUNK_SIZE*MULTIPLIER
13
 
 
14
 
### assign to 'me' this processor's ID
15
 
### assign to 'nproc' how many processors in the world
16
 
 
17
 
class Task(object):
18
 
    def __init__(self, alo, ahi, blo, bhi, clo, chi):
19
 
        self.alo = alo
20
 
        self.ahi = ahi
21
 
        self.blo = blo
22
 
        self.bhi = bhi
23
 
        self.clo = clo
24
 
        self.chi = chi
25
 
    def __repr__(self):
26
 
        return "Task(%s,%s,%s,%s,%s,%s)" % (
27
 
                self.alo, self.ahi, self.blo, self.bhi, self.clo, self.chi)
28
 
 
29
 
def get_task_list(chunk_size, multiplier):
30
 
    count = 0
31
 
    task_list = [None]*multiplier**3
32
 
    for row_chunk in range(multiplier):
33
 
        for col_chunk in range(multiplier):
34
 
            clo = [ row_chunk   *chunk_size,  col_chunk   *chunk_size]
35
 
            chi = [(row_chunk+1)*chunk_size, (col_chunk+1)*chunk_size]
36
 
            for i in range(multiplier):
37
 
                alo = [ row_chunk   *chunk_size,  i   *chunk_size]
38
 
                ahi = [(row_chunk+1)*chunk_size, (i+1)*chunk_size]
39
 
                blo = [ i   *chunk_size,  col_chunk   *chunk_size]
40
 
                bhi = [(i+1)*chunk_size, (col_chunk+1)*chunk_size]
41
 
                task_list[count] = Task(alo, ahi, blo, bhi, clo, chi)
42
 
                count += 1
43
 
    return task_list
44
 
 
45
 
def srumma(g_a, g_b, g_c, chunk_size, multiplier):
46
 
    # statically partition the task list among nprocs
47
 
    task_list = get_task_list(chunk_size, multiplier)
48
 
    ntasks = multiplier**3 // nproc
49
 
    start = me*ntasks
50
 
    stop = (me+1)*ntasks
51
 
    if me+1 == nproc:
52
 
        stop += multiplier**3 % nproc
53
 
    # the srumma algorithm, more or less
54
 
    task_prev = task_list[start]
55
 
    ### use a nonblocking get to request first block and nb handle from 'g_a'
56
 
    ###     and assign to 'a_prev' and 'a_nb_prev'
57
 
    ### use a nonblocking get to request first block and nb handle from 'g_b'
58
 
    ###     and assign to 'b_prev' and 'b_nb_prev'
59
 
    for i in range(start+1,stop):
60
 
        task_next = task_list[i]
61
 
        ### use a nonblocking get to request next block and nb handle from 'g_a'
62
 
        ###     and assign to 'a_next' and 'a_nb_next'
63
 
        ### use a nonblocking get to request next block and nb handle from 'g_b'
64
 
        ###     and assign to 'b_next' and 'b_nb_next'
65
 
        ### wait on the previoius nb handle for 'g_a'
66
 
        ### wait on the previoius nb handle for 'g_b'
67
 
        result = np.dot(a_prev,b_prev)
68
 
        ### accumulate the result into 'g_c' at the previous block location
69
 
        task_prev = task_next
70
 
        a_prev,a_nb_prev = a_next,a_nb_next
71
 
        b_prev,b_nb_prev = b_next,b_nb_next
72
 
    ### wait on the previoius nb handle for 'g_a'
73
 
    ### wait on the previoius nb handle for 'g_b'
74
 
    ga.nbwait(a_nb_prev)
75
 
    ga.nbwait(b_nb_prev)
76
 
    result = np.dot(a_prev,b_prev)
77
 
    ### accumulate the result into 'g_c' at the previous block location
78
 
    ### synchronize
79
 
 
80
 
def verify_using_ga(g_a, g_b, g_c):
81
 
    g_v = ga.duplicate(g_c)
82
 
    ga.gemm(False,False,N,N,N,1,g_a,g_b,0,g_v)
83
 
    c = ga.access(g_c)
84
 
    v = ga.access(g_v)
85
 
    if c is not None:
86
 
        val = int(np.abs(np.sum(c-v))>0.0001)
87
 
    else:
88
 
        val = 0
89
 
    val = ga.gop_add(val)
90
 
    ga.destroy(g_v)
91
 
    return val == 0
92
 
 
93
 
def verify_using_np(g_a, g_b, g_c):
94
 
    a = ga.get(g_a)
95
 
    b = ga.get(g_b)
96
 
    c = ga.get(g_c)
97
 
    v = np.dot(a,b)
98
 
    val = int(np.abs(np.sum(c-v))>0.0001)
99
 
    val = ga.gop_add(val)
100
 
    return val == 0
101
 
 
102
 
if __name__ == '__main__':
103
 
    if nproc > MULTIPLIER**3:
104
 
        if 0 == me:
105
 
            print "You must use less than %s processors" % (MULTIPLIER**3+1)
106
 
    else:
107
 
        g_a = ga.create(ga.C_DBL, [N,N])
108
 
        g_b = ga.create(ga.C_DBL, [N,N])
109
 
        g_c = ga.create(ga.C_DBL, [N,N])
110
 
        # put some fake data into input arrays A and B
111
 
        if me == 0:
112
 
            ga.put(g_a, np.random.random(N*N))
113
 
            ga.put(g_b, np.random.random(N*N))
114
 
        ga.sync()
115
 
        if me == 0:
116
 
            print "srumma...",
117
 
        srumma(g_a, g_b, g_c, CHUNK_SIZE, MULTIPLIER)
118
 
        if me == 0:
119
 
            print "done"
120
 
        if me == 0:
121
 
            print "verifying using ga.gemm...",
122
 
        ok = verify_using_ga(g_a, g_b, g_c)
123
 
        if me == 0:
124
 
            if ok:
125
 
                print "OKAY"
126
 
            else:
127
 
                print "FAILED"
128
 
        if me == 0:
129
 
            print "verifying using np.dot...",
130
 
        ok = verify_using_np(g_a, g_b, g_c)
131
 
        if me == 0:
132
 
            if ok:
133
 
                print "OKAY"
134
 
            else:
135
 
                print "FAILED"
136
 
        ga.destroy(g_a)
137
 
        ga.destroy(g_b)
138
 
        ga.destroy(g_c)