rename converse pipelined broadcast files
[charm.git] / src / conv-com / pipebroadcastconverse.C
1 #include "pipebroadcastconverse.h"
2
3 //PipeBcastHashKey CODE
4 int PipeBcastHashKey::staticCompare(const void *k1,const void *k2,size_t ){
5     return ((const PipeBcastHashKey *)k1)->
6                 compare(*(const PipeBcastHashKey *)k2);
7 }
8
9 CkHashCode PipeBcastHashKey::staticHash(const void *v,size_t){
10     return ((const PipeBcastHashKey *)v)->hash();
11 }
12
13 void PipeBroadcastConverse::commonInit(){
14   log_of_2_inv = 1/log((double)2);
15   seqNumber = 0;
16 }
17
18 extern void propagate_handler(void *);
19
20 void propagate_handler_frag(void *message) {
21   int instid = CmiGetXHandler(message);
22   PipeBroadcastConverse *myStrategy = (PipeBroadcastConverse*)ConvComlibGetStrategy(instid);
23   //CProxy_ComlibManager(CkpvAccess(cmgrID)).ckLocalBranch()->getStrategy(instid);
24   PipeBcastInfo *info = (PipeBcastInfo*)(((char*)message)+CmiReservedHeaderSize);
25   myStrategy->propagate((char*)message, true, info->srcPe, info->chunkSize+CmiReservedHeaderSize+sizeof(PipeBcastInfo), NULL);
26 }
27
28 void PipeBroadcastConverse::propagate(char *env, int isFragmented, int srcPeNumber, int totalSendingSize, setFunction setPeNumber){
29   // find destination processors and send
30   int destination, tmp, k;
31   int num_pes, *dest_pes;
32   PipeBcastInfo *info = (PipeBcastInfo*)(env+CmiReservedHeaderSize);
33   //int srcPeNumber = isFragmented ? info->srcPe : env->getSrcPe();
34   //int totalSendingSize = isFragmented ? info->chunkSize+CmiReservedHeaderSize+sizeof(PipeBcastInfo) : env->getTotalsize();
35
36   switch (topology) {
37   case USE_LINEAR:
38     if (srcPeNumber == (CmiMyPe()+1)%CmiNumPes()) break;
39     destination = (CmiMyPe()+1) % CmiNumPes();
40     ComlibPrintf("[%d] Pipebroadcast sending to %d\n",CmiMyPe(), destination);
41     CmiSyncSend(destination, totalSendingSize, env);
42     break;
43   case USE_HYPERCUBE:
44     tmp = srcPeNumber ^ CmiMyPe();
45     k = int(log((double)CmiNumPes()) * log_of_2_inv + 2);
46     if (tmp) {
47       do {--k;} while (!(tmp>>k));
48     }
49     ComlibPrintf("[%d] tmp=%d, k=%d\n",CmiMyPe(),tmp,k);
50     // now 'k' is the last dimension in the hypercube used for exchange
51     if (isFragmented) info->srcPe = CmiMyPe();
52     else setPeNumber(env,CmiMyPe());  // where the message is coming from
53     dest_pes = (int *)malloc(k*sizeof(int));
54     --k;  // next dimension in the cube to be used
55     num_pes = HypercubeGetBcastDestinations(k, dest_pes);
56
57     /*
58     for ( ; k>=0; --k) {
59       // add the processor destination at level k if it exist
60       dest_pes[num_pes] = CmiMyPe() ^ (1<<k);
61       if (dest_pes[num_pes] >= CmiNumPes()) {
62         dest_pes[num_pes] &= (-1)<<k;
63         if (CmiNumPes()>dest_pes[num_pes]) dest_pes[num_pes] += (CmiMyPe() - (CmiMyPe() & ((-1)<<k))) % (CmiNumPes() - dest_pes[num_pes]);
64       }
65       if (dest_pes[num_pes] < CmiNumPes()) {
66         ComlibPrintf("[%d] PipeBroadcast sending to %d\n",CmiMyPe(), dest_pes[num_pes]);
67         ++num_pes;
68       }
69     }
70     */
71
72     //CmiSyncListSend(num_pes, dest_pes, env->getTotalsize(), (char *)env);
73     for (k=0; k<num_pes; ++k) CmiSyncSend(dest_pes[k], totalSendingSize, env);
74     free(dest_pes);
75     break;
76
77     // for other strategies
78
79   default:
80     // should NEVER reach here!
81     CmiPrintf("Error, topology %d not known\n",topology);
82     CkExit();
83   }
84
85   // deliver messages to local objects (i.e. send it to ComlibManager)
86   storing(env, isFragmented);
87   //CmiSetHandler(env, CmiGetXHandler(env));
88   //CmiSyncSendAndFree(CkMyPe(), env->getTotalsize(), (char *)env);
89
90 }
91
92 void PipeBroadcastConverse::storing(char* fragment, int isFragmented) {
93   char *complete;
94   int isFinished=0;
95   //ComlibPrintf("isArray = %d\n", (getType() == ARRAY_STRATEGY));
96
97   // check if the message is fragmented
98   if (isFragmented) {
99     // store the fragment in the hash table until completed
100     ComlibPrintf("[%d] deliverer: received fragmented message, storing\n",CkMyPe());
101     PipeBcastInfo *info = (PipeBcastInfo*)(fragment+CmiReservedHeaderSize);
102
103     PipeBcastHashKey key (info->bcastPe, info->seqNumber);
104     PipeBcastHashObj *position = fragments.get(key);
105
106     char *incomingMsg;
107     if (position) {
108       // the message already exist, add to it
109       ComlibPrintf("[%d] adding to an existing message for id %d/%d (%d remaining)\n",CmiMyPe(),info->bcastPe,info->seqNumber,position->remaining-1);
110       incomingMsg = position->message;
111       memcpy (incomingMsg+CmiReservedHeaderSize+((pipeSize-CmiReservedHeaderSize-sizeof(PipeBcastInfo))*info->chunkNumber), fragment+CmiReservedHeaderSize+sizeof(PipeBcastInfo), info->chunkSize);
112
113       if (--position->remaining == 0) {  // message completely received
114         isFinished = 1;
115         complete = incomingMsg;
116         // delete from the hash table
117         fragments.remove(key);
118       }
119
120     } else {
121       // the message doesn't exist, create it
122       ComlibPrintf("[%d] creating new message of size %d for id %d/%d; chunk=%d chunkSize=%d\n",CmiMyPe(),info->messageSize,info->bcastPe,info->seqNumber,info->chunkNumber,info->chunkSize);
123       incomingMsg = (char*)CmiAlloc(info->messageSize);
124       memcpy (incomingMsg, fragment, CmiReservedHeaderSize);
125       memcpy (incomingMsg+CmiReservedHeaderSize+((pipeSize-CmiReservedHeaderSize-sizeof(PipeBcastInfo))*info->chunkNumber), fragment+CmiReservedHeaderSize+sizeof(PipeBcastInfo), info->chunkSize);
126       int remaining = (int)ceil((double)info->messageSize/(pipeSize-CmiReservedHeaderSize-sizeof(PipeBcastInfo)))-1;
127       if (remaining) {  // more than one chunk (it was not forced to be splitted)
128         PipeBcastHashObj *object = new PipeBcastHashObj(info->messageSize, remaining, incomingMsg);
129         fragments.put(key) = object;
130       } else {  // only one chunk, it was forces to be splitted
131         isFinished = 1;
132         complete = incomingMsg;
133         // nothing to delete from fragments since nothing has been added
134       }
135     }
136     CmiFree(fragment);
137
138   } else {  // message not fragmented
139     ComlibPrintf("[%d] deliverer: received message in single chunk\n",CmiMyPe());
140     isFinished = 1;
141     complete = fragment;
142   }
143
144   if (isFinished) {
145     higherLevel->deliverer(complete);
146   }
147 }
148
149 void PipeBroadcastConverse::deliverer(char *msg) {
150   // TO BE DONE
151 }
152
153 PipeBroadcastConverse::PipeBroadcastConverse(int _topology, int _pipeSize, Strategy *parent) : Strategy(), topology(_topology), pipeSize(_pipeSize) {
154   higherLevel = parent;
155   seqNumber = 0;
156   ComlibPrintf("init: %d %d\n",topology, pipeSize);
157 }
158
159 void PipeBroadcastConverse::insertMessage(MessageHolder *cmsg){
160   ComlibPrintf("[%d] Pipelined Broadcast with converse strategy %d\n",CkMyPe(),topology);
161   messageBuf->enq(cmsg);
162   doneInserting();
163 }
164
165 void PipeBroadcastConverse::doneInserting(){
166   ComlibPrintf("[%d] DoneInserting\n",CkMyPe());
167   while (!messageBuf->isEmpty()) {
168     MessageHolder *cmsg = messageBuf->deq();
169     // modify the Handler to deliver the message to the propagator
170     char *env = cmsg->getMessage();
171
172     //conversePipeBcast(env, env->getTotalsize(), false);
173   }
174 }
175
176 // routine for interfacing with converse.
177 // Require only the converse reserved header if forceSplit is true
178 void PipeBroadcastConverse::conversePipeBcast(char *env, int totalSize) {
179   // set the instance ID to be used by the receiver using the XHandler variable
180   CmiSetXHandler(env, myInstanceID);
181
182   ++seqNumber;
183   // message doesn't fit into the pipe: split it into chunks and propagate them individually
184   ComlibPrintf("[%d] Propagating message in multiple chunks (totalsize=%d)\n",CmiMyPe(),totalSize);
185
186   char *sendingMsg;
187   char *nextChunk = env+CmiReservedHeaderSize;
188   int remaining = totalSize-CmiReservedHeaderSize;
189   int reducedPipe = pipeSize-CmiReservedHeaderSize-sizeof(PipeBcastInfo);
190   ComlibPrintf("reducedPipe = %d, CmiReservedHeaderSize = %d, sizeof(PipeBcastInfo) = %d\n",reducedPipe,CmiReservedHeaderSize,sizeof(PipeBcastInfo));
191   ComlibPrintf("sending %d chunks of size %d, total=%d\n",(int)ceil(((double)totalSize-CmiReservedHeaderSize)/reducedPipe),reducedPipe,remaining);
192   for (int i=0; i<(int)ceil(((double)totalSize-CmiReservedHeaderSize)/reducedPipe); ++i) {
193     sendingMsg = (char*)CmiAlloc(pipeSize);
194     CmiSetHandler(env, propagateHandle_frag);
195     memcpy (sendingMsg, env, CmiReservedHeaderSize);
196     PipeBcastInfo *info = (PipeBcastInfo*)(sendingMsg+CmiReservedHeaderSize);
197     info->srcPe = CmiMyPe();
198     info->bcastPe = CmiMyPe();
199     info->seqNumber = seqNumber;
200     info->chunkNumber = i;
201     info->chunkSize = reducedPipe<remaining ? reducedPipe : remaining;
202     info->messageSize = totalSize;
203     memcpy (sendingMsg+CmiReservedHeaderSize+sizeof(PipeBcastInfo), nextChunk, reducedPipe);
204
205     remaining -= reducedPipe;
206     nextChunk += reducedPipe;
207
208     propagate(sendingMsg, true, CmiMyPe(), totalSize, NULL);
209   }
210 }
211
212 void PipeBroadcastConverse::pup(PUP::er &p){
213   Strategy::pup(p);
214   ComlibPrintf("[%d] initial of Pipeconverse pup %s\n",CmiMyPe(),(p.isPacking()==0)?(p.isUnpacking()?"UnPacking":"sizer"):("Packing"));
215
216   p | pipeSize;
217   p | topology;
218   p | seqNumber;
219
220   ComlibPrintf("[%d] PipeBroadcast converse pupping %s, size=%d, topology=%d\n",CmiMyPe(), (p.isPacking()==0)?(p.isUnpacking()?"UnPacking":"sizer"):("Packing"),pipeSize,topology);
221
222   if (p.isUnpacking()) {
223     log_of_2_inv = 1/log((double)2);
224     messageBuf = new CkQ<MessageHolder *>;
225     propagateHandle_frag = CmiRegisterHandler((CmiHandler)propagate_handler_frag);
226   }
227   //p|(*messageBuf);
228   //p|fragments;
229
230 }
231
232 PUPable_def(PipeBroadcastConverse);