2
//@+node:gcross.20081019105739.3:@thin blacs_grid.h
13
#include "pup_cmialloc.h"
16
#include "C++BLAS/c++pblas.h"
17
#include "ampi_controller.h"
19
#include "blacs_grid.decl.h"
22
void BLACS_initialize_thread_variable();
23
void BLACS_initialize();
27
//@+node:gcross.20081019105739.12:BLACSGridSlave Declaration
28
class BLACSGridSlave : public AMPISlave {
32
int number_of_rows, number_of_columns;
35
BLACSGridSlave(int number_of_rows_, int number_of_columns_) :
36
AMPISlave(number_of_rows_*number_of_columns_),
37
number_of_rows(number_of_rows_),
38
number_of_columns(number_of_columns_)
41
BLACSGridSlave(CkMigrateMessage* msg) : AMPISlave(msg) { }
43
static BLACSGridSlave* getThread() { return dynamic_cast<BLACSGridSlave*>(AMPISlave::getThread()); }
47
virtual void initialize();
50
//@-node:gcross.20081019105739.12:BLACSGridSlave Declaration
51
//@+node:gcross.20081030170932.4:UpdateMatrixDescriptorOperation
52
template<typename T_numtype> class UpdateMatrixDescriptorOperation : public AbstractOperation<T_numtype,2> {
54
PUPable_decl_template(UpdateMatrixDescriptorOperation)
58
typedef AbstractOperation<T_numtype,2> BaseType;
59
typedef DistributedArraySegment<T_numtype,2> ArraySegmentType;
61
UpdateMatrixDescriptorOperation() { }
63
UpdateMatrixDescriptorOperation(CkMigrateMessage* msg) { }
65
virtual void perform_on(ArraySegmentType& segment) {
66
segment.descriptor = descriptor;
67
segment.descriptor.leading_dimension() = segment.rows();
70
virtual void pup(PUP::er &p) {
75
Descriptor descriptor;
78
//@-node:gcross.20081030170932.4:UpdateMatrixDescriptorOperation
79
//@+node:gcross.20081019105739.4:BLACSGridMaster Declaration
80
class BLACSGridMaster : public AMPIMaster {
84
friend class BLACSGridSlave;
85
typedef TinyVector<int,2> Coordinates;
87
const Coordinates PE_dimensions;
91
//CProxy_BLACSGridThreadArray thread_array_proxy;
93
BLACSGridMaster(int number_of_rows, int number_of_columns);
95
//@ << inner class DistributedMatrix >>
96
//@+node:gcross.20081025093011.2:<< inner class DistributedMatrix >>
97
template<typename T_numtype> class DistributedMatrix : public BlockCyclicArray<T_numtype,2> {
99
typedef BlockCyclicArray<T_numtype,2> BaseType;
100
BLACSGridMaster& parent;
101
DistributedMatrix(BLACSGridMaster& parent_, int number_of_rows, int number_of_columns, int row_block_size, int column_block_size) :
102
BaseType(parent_.PE_dimensions,Coordinates(number_of_rows,number_of_columns),Coordinates(row_block_size,column_block_size),fortranArray,parent_.thread_array_proxy),
105
UpdateMatrixDescriptorOperation<T_numtype> op;
106
op.descriptor.type() = 1;
107
op.descriptor.number_of_rows() = number_of_rows;
108
op.descriptor.number_of_columns() = number_of_columns;
109
op.descriptor.row_blocking_factor() = row_block_size;
110
op.descriptor.column_blocking_factor() = column_block_size;
111
op.descriptor.first_process_row() = 0;
112
op.descriptor.first_process_column() = 0;
113
broadcast_operation(op);
116
using BaseType::operator=;
118
//@-node:gcross.20081025093011.2:<< inner class DistributedMatrix >>
123
//@-node:gcross.20081019105739.4:BLACSGridMaster Declaration
124
//@+node:gcross.20081030231935.10:Operations
125
//@+node:gcross.20081030231935.6:AbstractDistributedMatrixOperation
126
template<typename T_numtype> class AbstractDistributedMatrixOperation : public DistributedArraySegmentOperation<T_numtype,2> {
130
typedef DistributedArraySegmentOperation<T_numtype,2> BaseType;
131
typedef DistributedArraySegment<T_numtype,2> ArraySegmentType;
133
friend class PUP::able;
135
AbstractDistributedMatrixOperation() { }
137
AbstractDistributedMatrixOperation(int operation_id_, CProxy_BLACSGridSlave grid_proxy_, int number_of_arguments_, int argument_position_) :
138
operation_id(operation_id_),
139
grid_proxy(grid_proxy_),
140
number_of_arguments(number_of_arguments_),
141
argument_position(argument_position_)
144
AbstractDistributedMatrixOperation(CkMigrateMessage* msg) { }
146
CProxy_BLACSGridSlave grid_proxy;
148
int number_of_arguments, argument_position;
150
ArraySegmentType* segment;
152
static void Function(vector<Operation*>& arguments) {
153
for(int i = 0; i < arguments.size(); i ++) {
154
dynamic_cast<AbstractDistributedMatrixOperation*>(arguments[i])->segment->descriptor.context() = BLACSGridSlave::getThread()->blacs_context;
156
dynamic_cast<AbstractDistributedMatrixOperation*>(arguments[0])->operate(arguments);
159
virtual void operate(vector<Operation*>& arguments) = 0;
163
virtual bool perform(ArraySegmentType& segment_) {
165
BLACSGridSlave* thread = grid_proxy[segment_.thisIndex].ckLocal();
166
CkAssert(thread != NULL);
167
thread->perform_synchronized_operation(operation_id,number_of_arguments,argument_position,this,Function);
171
virtual void pup(PUP::er &p) {
175
p|number_of_arguments;
181
//@-node:gcross.20081030231935.6:AbstractDistributedMatrixOperation
182
//@+node:gcross.20081030231935.7:TransposeOperation
183
template<typename T_numtype> class TransposeOperation : public AbstractDistributedMatrixOperation<T_numtype> {
185
PUPable_decl_template(TransposeOperation)
189
virtual void operate(vector<Operation*>& arguments) {
190
CkAssert(arguments.size()==2);
191
TransposeOperation& A = *dynamic_cast<TransposeOperation*>(arguments[0]);
192
TransposeOperation& C = *dynamic_cast<TransposeOperation*>(arguments[1]);
193
cout << "BEFORE" << endl;
194
cout << "A=" << (*A.segment) << endl;
195
cout << "C=" << (*C.segment) << endl;
197
C.segment->descriptor.number_of_rows(),
198
C.segment->descriptor.number_of_columns(),
199
A.coefficient,A.segment->data(),1,1,A.segment->descriptor,
200
C.coefficient,C.segment->data(),1,1,C.segment->descriptor
202
cout << "AFTER" << endl;
203
cout << "A=" << (*A.segment) << endl;
204
cout << "C=" << (*C.segment) << endl;
207
T_numtype coefficient;
211
typedef AbstractDistributedMatrixOperation<T_numtype> BaseType;
212
typedef DistributedArraySegment<T_numtype,2> ArraySegmentType;
214
TransposeOperation(int operation_id_, CProxy_BLACSGridSlave grid_proxy_,int argument_position_, T_numtype coefficient_) :
215
BaseType(operation_id_,grid_proxy_,2,argument_position_),
216
coefficient(coefficient_)
219
TransposeOperation(CkMigrateMessage* msg) { }
222
virtual void pup(PUP::er &p) {
228
//@-node:gcross.20081030231935.7:TransposeOperation
229
//@+node:gcross.20081102101609.2:SolveOperation
230
template<typename T_numtype> class SolveOperation : public AbstractDistributedMatrixOperation<T_numtype> {
232
PUPable_decl_template(SolveOperation)
236
virtual void operate(vector<Operation*>& arguments) {
237
CkAssert(arguments.size()==2);
238
SolveOperation& A = *dynamic_cast<SolveOperation*>(arguments[0]);
239
SolveOperation& B = *dynamic_cast<SolveOperation*>(arguments[1]);
240
CkAssert(A.segment->descriptor.number_of_rows()==A.segment->descriptor.number_of_columns());
242
int IPIV[100]; // just for testing!!!!
244
int info = PBLAS::solve(
245
A.segment->descriptor.number_of_rows(),
246
B.segment->descriptor.number_of_columns(),
247
A.segment->data(),1,1,A.segment->descriptor,
249
B.segment->data(),1,1,B.segment->descriptor
257
typedef AbstractDistributedMatrixOperation<T_numtype> BaseType;
258
typedef DistributedArraySegment<T_numtype,2> ArraySegmentType;
260
SolveOperation(int operation_id_, CProxy_BLACSGridSlave grid_proxy_,int argument_position_) :
261
BaseType(operation_id_,grid_proxy_,2,argument_position_)
264
SolveOperation(CkMigrateMessage* msg) { }
267
virtual void pup(PUP::er &p) {
272
//@-node:gcross.20081102101609.2:SolveOperation
273
//@-node:gcross.20081030231935.10:Operations
274
//@+node:gcross.20081030231935.8:Functions
275
//@+node:gcross.20081030231935.9:transpose
276
template<typename T> void transpose(BLACSGridMaster::DistributedMatrix<T>& A,BLACSGridMaster::DistributedMatrix<T>& C) {
277
CkAssert(&A.parent==&C.parent);
279
BLACSGridMaster& grid = A.parent;
281
int operation_id = grid.allocate_next_operation_id();
283
TransposeOperation<T> A_op(operation_id, grid.thread_array_proxy, 0, 1.0);
284
A.broadcast_operation(A_op);
286
TransposeOperation<T> C_op(operation_id, grid.thread_array_proxy, 1, 0.0);
287
C.broadcast_operation(C_op);
289
//@-node:gcross.20081030231935.9:transpose
290
//@+node:gcross.20081102101609.4:solve
291
template<typename T> void solve(BLACSGridMaster::DistributedMatrix<T>& A,BLACSGridMaster::DistributedMatrix<T>& B) {
292
CkAssert(&A.parent==&B.parent);
293
CkAssert(A.rows()==A.columns());
294
CkAssert(A.columns()==B.rows());
296
BLACSGridMaster& grid = A.parent;
298
int operation_id = grid.allocate_next_operation_id();
300
SolveOperation<T> A_op(operation_id, static_cast<CProxy_BLACSGridSlave&>(grid.thread_array_proxy), 0);
301
A.broadcast_operation(A_op);
303
SolveOperation<T> B_op(operation_id, static_cast<CProxy_BLACSGridSlave&>(grid.thread_array_proxy), 1);
304
B.broadcast_operation(B_op);
307
//@-node:gcross.20081102101609.4:solve
308
//@-node:gcross.20081030231935.8:Functions
311
#endif // BLACS_GRID_H
312
//@-node:gcross.20081019105739.3:@thin blacs_grid.h