summaryrefslogtreecommitdiff
blob: 09f57b3ae55ca29841f153ed6925172a9ed92806 (plain)
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
//=====================================================
// Copyright (C) 2011 Andrea Arteaga <andyspiros@gmail.com>
//=====================================================
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
#ifndef BTL_BLACS_INTERFACE_H
#define BTL_BLACS_INTERFACE_H

#include <vector>
#include <algorithm>
#include "blacs.h"
extern "C" {
  void descinit_(int*, const int*, const int*, const int*, const int*, const int*, const int*, const int*, const int*, int*);
  int numroc_(const int*, const int*, const int*, const int*, const int*);
}

#include "scatter.h"
#include "gather.h"

template<typename real>
class blacs_interface
{

public:
  typedef real real_type;
  typedef std::vector<real_type> stl_vector;
  typedef stl_vector stl_matrix;

  typedef real* gene_matrix;
  typedef real* gene_vector;


  static void free_matrix(gene_matrix & A, int N){
    delete A;
  }

  static void free_vector(gene_vector & B){
    delete B;
  }

  static inline void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
    int N = A_stl.size();
    A = new real[N];
    for (int j=0;j<N;j++)
        A[j] = A_stl[j];
  }

  static inline void vector_from_stl(gene_vector & B, stl_vector & B_stl){
    int N = B_stl.size();
    B = new real[N];
    for (int i=0;i<N;i++)
      B[i] = B_stl[i];
  }

  static inline void vector_to_stl(gene_vector & B, stl_vector & B_stl){
    int N = B_stl.size();
    for (int i=0;i<N;i++)
      B_stl[i] = B[i];
  }

  static inline void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
    int N = A_stl.size();
    for (int i=0;i<N;i++)
      A_stl[i] = A[i];
  }

  static inline void copy_vector(const gene_vector & source, gene_vector & cible, int N){
    for (int i=0;i<N;i++)
      cible[i]=source[i];
  }

  static inline void copy_matrix(const gene_matrix & source, gene_matrix & cible, int N){
    for (int i=0;i<N;i++)
      cible[i]=source[i];
  }


public:
  static int context() {
    int ctxt, ignored, what = 0;
    blacs_get_(&ignored, &what, &ctxt);
    return ctxt;
  }
  static int myid() {
    int procnum, myid;
    blacs_pinfo_(&myid, &procnum);
    return myid;
  }


  static void scatter_matrix(const stl_vector& GlobalMatrix, stl_vector& LocalMatrix,
      int& GlobalRows, int& GlobalCols,
      int&  BlockRows, int&  BlockCols,
      int&  LocalRows, int&  LocalCols
  ) {
    scatter(context(), GlobalMatrix, LocalMatrix, GlobalRows, GlobalCols, BlockRows, BlockCols, LocalRows, LocalCols);
  }

  static void scatter_matrix(const stl_vector& GlobalMatrix, stl_vector& LocalMatrix,
      int *desc,
      const int& GlobalRows=0, const int& GlobalCols=0,
      const int& BlockRows=0, const int& BlockCols=0
  ) {
    int GlobalRows_ = GlobalRows, GlobalCols_ = GlobalCols,
        BlockRows_ = BlockRows, BlockCols_ = BlockCols,
        LocalRows_, LocalCols_;
    const int ctxt = context();
    scatter(ctxt, GlobalMatrix, LocalMatrix,
        GlobalRows_, GlobalCols_, BlockRows_, BlockCols_, LocalRows_, LocalCols_
    );

    const int iZERO = 0;
    int info;
    const int LLD = std::max(1, LocalRows_);
    descinit_(desc, &GlobalRows_, &GlobalCols_, &BlockRows_, &BlockCols_,
        &iZERO, &iZERO, &ctxt, &LLD, &info
    );
  }

  static void gather_matrix(stl_vector& GlobalMatrix, const stl_vector& LocalMatrix,
      int& GlobalRows, int& GlobalCols,
      int&  BlockRows, int&  BlockCols,
      int&  LocalRows, int&  LocalCols
  ) {
    gather(context(), GlobalMatrix, LocalMatrix, GlobalRows, GlobalCols, BlockRows, BlockCols, LocalRows, LocalCols);
  }

  static void gather_matrix(stl_vector& GlobalMatrix, const stl_vector& LocalMatrix,
      int* desc
  ) {
    int GlobalRows = desc[2], GlobalCols = desc[3],
        BlockRows = desc[4], BlockCols = desc[5],
        LocalRows = desc[8], LocalCols = LocalMatrix.size()/desc[8];
    const int ctxt = context();
    gather(ctxt, GlobalMatrix, LocalMatrix, GlobalRows, GlobalCols, BlockRows, BlockCols, LocalRows, LocalCols);
  }


};

#endif /* BTL_BLACS_INTERFACE_H */