00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _CKSPARSECONTIGUOUSREDUCER_H
00011 #define _CKSPARSECONTIGUOUSREDUCER_H
00012
00013 #include "CkSparseContiguousReducer.decl.h"
00014
00015
00016
00017
00018 extern CkReduction::reducerType sparse_sum_int;
00019 extern CkReduction::reducerType sparse_sum_float;
00020 extern CkReduction::reducerType sparse_sum_double;
00021 extern CkReduction::reducerType sparse_sum_TwoFloats;
00022 extern CkReduction::reducerType sparse_sum_TwoDoubles;
00023
00024 extern CkReduction::reducerType sparse_product_int;
00025 extern CkReduction::reducerType sparse_product_float;
00026 extern CkReduction::reducerType sparse_product_double;
00027
00028 extern CkReduction::reducerType sparse_max_int;
00029 extern CkReduction::reducerType sparse_max_float;
00030 extern CkReduction::reducerType sparse_max_double;
00031
00032 extern CkReduction::reducerType sparse_min_int;
00033 extern CkReduction::reducerType sparse_min_float;
00034 extern CkReduction::reducerType sparse_min_double;
00035
00036 class CkTwoDoubles{
00037 public:
00038 double d1, d2;
00039 CkTwoDoubles(double _d1=0, double _d2=0) :d1(_d1), d2(_d2){}
00040 void operator+=(CkTwoDoubles d){d1+=d.d1; d2+=d.d2;}
00041 };
00042
00043 class CkTwoFloats{
00044 public:
00045 float f1, f2;
00046 CkTwoFloats(float _f1=0, float _f2=0) :f1(_f1), f2(_f2){}
00047 void operator+=(CkTwoFloats f){f1+=f.f1; f2+=f.f2;}
00048 };
00049
00050 class CkDataSegHeader{
00051 public:
00052
00053
00054
00055
00056 int sx, sy, ex, ey;
00057
00058 CkDataSegHeader(){
00059 }
00060
00061 CkDataSegHeader(int _sx, int _sy, int _ex, int _ey): sx(_sx), sy(_sy), ex(_ex), ey(_ey) {}
00062
00063 CkDataSegHeader(const CkDataSegHeader &r) : sx(r.sx), sy(r.sy), ex(r.ex), ey(r.ey) {}
00064
00065
00066 inline bool operator==(const CkDataSegHeader &r){
00067 if((r.sx == sx)&&(r.sy == sy)&&(r.ex == ex)&&(r.ey == ey))
00068 return true;
00069
00070 return false;
00071 }
00072
00073
00074
00075
00076 inline bool operator<(const CkDataSegHeader &r){
00077 if(sx < r.sx)
00078 return true;
00079 else
00080 if((sx == r.sx)&&(ex < r.ex))
00081 return true;
00082 else
00083 if((sx == r.sx)&&(ex == r.ex)&&(sy < r.sy))
00084 return true;
00085 else
00086 if((sx == r.sx)&&(ex == r.ex)&&(sy == r.sy)&&(ey < r.ey))
00087 return true;
00088
00089 return false;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 inline int getNumElements(){
00106 return (ex-sx+1)*(ey-sy+1);
00107 }
00108 };
00109
00110
00111
00112
00113
00114 template <class T>
00115 class CkSparseContiguousReducer
00116 {
00117 CkDataSegHeader r;
00118 T *data;
00119
00120 public:
00121 CkSparseContiguousReducer(){}
00122 ~CkSparseContiguousReducer(){}
00123
00124
00125
00126
00127
00128
00129 inline void add(int _sx, int _sy, int _ex, int _ey, T *_data){
00130 r.sx = _sx;
00131 r.sy = _sy;
00132 r.ex = _ex;
00133 r.ey = _ey;
00134 data = _data;
00135 }
00136
00137
00138
00139
00140
00141 void contribute(ArrayElement *elem, CkReduction::reducerType type, const
00142 CkCallback &cb){
00143 int dataAlignSize = sizeof(int)*2 + sizeof(CkDataSegHeader);
00144 if(dataAlignSize%sizeof(double)) {
00145 dataAlignSize += sizeof(double) - (dataAlignSize%sizeof(double));
00146 }
00147
00148 int size = r.getNumElements()*sizeof(T) + dataAlignSize;
00149 CkReductionMsg* msg = CkReductionMsg::buildNew (size, NULL, type);
00150 msg->setCallback(cb);
00151
00152 unsigned char *ptr = (unsigned char*)(msg->getData());
00153 int count = 1;
00154
00155 memcpy(ptr, &count, sizeof(int));
00156 memcpy(ptr+sizeof(int), &dataAlignSize, sizeof(int));
00157 memcpy(ptr + 2*sizeof(int), &r, sizeof(CkDataSegHeader));
00158 memcpy(ptr + 2*sizeof(int) + sizeof(CkDataSegHeader), data,
00159 r.getNumElements()*sizeof(T));
00160
00161 elem->contribute(msg);
00162 }
00163
00164
00165
00166
00167
00168 void contribute(ArrayElement *elem, CkReduction::reducerType type){
00169 CkCallback cb;
00170 contribute (elem, type, cb);
00171 }
00172 };
00173
00174
00175
00176
00177 int numDataSegs(const unsigned char *data);
00178
00179
00180
00181
00182
00183 CkDataSegHeader getDataSegHeader(int index, const unsigned char *data);
00184
00185
00186
00187
00188
00189 CkDataSegHeader* getDataSegHeaderPtr(const unsigned char *data);
00190
00191
00192
00193
00194
00195
00196 unsigned char * getDataPtr(unsigned char *ptr);
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 int *decompressMsg(CkReductionMsg *m, CkDataSegHeader &h, int nullVal);
00208
00209 float *decompressMsg(CkReductionMsg *m, CkDataSegHeader &h, float nullVal);
00210
00211 double *decompressMsg(CkReductionMsg *m, CkDataSegHeader &h, double nullVal);
00212
00213 CkTwoDoubles *decompressMsg(CkReductionMsg *m, CkDataSegHeader &h, CkTwoDoubles nullVal);
00214
00215 CkTwoFloats *decompressMsg(CkReductionMsg *m, CkDataSegHeader &h, CkTwoFloats nullVal);
00216
00217 #endif