new random balancer; uses bit vector for active PEs.
[charm.git] / src / conv-ldb / cldb.test.c
1 #ifdef WIN32
2 #include "queueing.h"
3 extern void CldRestoreHandler(char *);
4 extern void CqsEnqueueGeneral(Queue, void *, unsigned int, unsigned int, unsigned int*);
5 extern void CldSwitchHandler(char *, int);
6 extern void CldModuleGeneralInit();
7 #endif
8
9 #include "converse.h"
10 #include "cldb.h"
11
12 void LoadNotifyFn(int l)
13 {
14 }
15
16 char *CldGetStrategy(void)
17 {
18   return "rand";
19 }
20
21 void CldBalanceHandler(void *msg)
22 {
23   CmiGrabBuffer((void **)&msg);
24   CldRestoreHandler(msg);
25   CldPutToken(msg);
26 }
27
28 void CldHandler(char *msg)
29 {
30   int len, queueing, priobits;
31   unsigned int *prioptr; 
32   CldInfoFn ifn; CldPackFn pfn;
33
34   CmiGrabBuffer((void **)&msg);
35   CldRestoreHandler(msg);
36   ifn = (CldInfoFn)CmiHandlerToFunction(CmiGetInfo(msg));
37   ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
38   CsdEnqueueGeneral(msg, queueing, priobits, prioptr);
39 }
40
41 void CldEnqueue(int pe, void *msg, int infofn)
42 {
43   int len, queueing, priobits; unsigned int *prioptr;
44   CldInfoFn ifn = (CldInfoFn)CmiHandlerToFunction(infofn);
45   CldPackFn pfn;
46
47   if (pe == CLD_ANYWHERE) {
48     pe = (((CrnRand()+CmiMyPe())&0x7FFFFFFF)%CmiNumPes());
49     while (!CldPresentPE(pe))
50       pe = (((CrnRand()+CmiMyPe())&0x7FFFFFFF)%CmiNumPes());
51     if (pe != CmiMyPe())
52       CpvAccess(CldRelocatedMessages)++;
53     if (pe == CmiMyPe()) {
54       ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
55       CmiSetInfo(msg,infofn);
56       CldPutToken(msg);
57     } 
58     else {
59       ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
60       if (pfn) {
61         pfn(&msg);
62         ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
63       }
64       CldSwitchHandler(msg, CpvAccess(CldBalanceHandlerIndex));
65       CmiSetInfo(msg,infofn);
66       CmiSyncSendAndFree(pe, len, msg);
67     }
68   }
69   else if ((pe == CmiMyPe()) || (CmiNumPes() == 1)) {
70     ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
71     CmiSetInfo(msg,infofn);
72     CsdEnqueueGeneral(msg, CQS_QUEUEING_LIFO, priobits, prioptr);
73   }
74   else {
75     ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
76     if (pfn) {
77       pfn(&msg);
78       ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
79     }
80     CldSwitchHandler(msg, CpvAccess(CldHandlerIndex));
81     CmiSetInfo(msg,infofn);
82     if (pe==CLD_BROADCAST) { CmiSyncBroadcastAndFree(len, msg); }
83     else if (pe==CLD_BROADCAST_ALL) { CmiSyncBroadcastAllAndFree(len, msg); }
84     else CmiSyncSendAndFree(pe, len, msg);
85   }
86 }
87
88 void CldNodeEnqueue(int node, void *msg, int infofn)
89 {
90   int len, queueing, priobits; unsigned int *prioptr;
91   CldInfoFn ifn = (CldInfoFn)CmiHandlerToFunction(infofn);
92   CldPackFn pfn;
93   if (node == CLD_ANYWHERE) {
94     /* node = (((rand()+CmiMyNode())&0x7FFFFFFF)%CmiNumNodes()); */
95     node = (((CrnRand()+CmiMyNode())&0x7FFFFFFF)%CmiNumNodes());
96     if (node != CmiMyNode())
97       CpvAccess(CldRelocatedMessages)++;
98   }
99   if (node == CmiMyNode()) {
100     ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
101     CsdNodeEnqueueGeneral(msg, queueing, priobits, prioptr);
102   } 
103   else {
104     ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
105     if (pfn) {
106       pfn(&msg);
107       ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
108     }
109     CldSwitchHandler(msg, CpvAccess(CldHandlerIndex));
110     CmiSetInfo(msg,infofn);
111     if (node==CLD_BROADCAST) { CmiSyncNodeBroadcastAndFree(len, msg); }
112     else if (node==CLD_BROADCAST_ALL){CmiSyncNodeBroadcastAllAndFree(len,msg);}
113     else CmiSyncNodeSendAndFree(node, len, msg);
114   }
115 }
116
117 void CldModuleInit()
118 {
119   CpvInitialize(int, CldHandlerIndex);
120   CpvInitialize(int, CldBalanceHandlerIndex);
121   CpvAccess(CldHandlerIndex) = CmiRegisterHandler(CldHandler);
122   CpvAccess(CldBalanceHandlerIndex) = CmiRegisterHandler(CldBalanceHandler);
123   CpvInitialize(int, CldRelocatedMessages);
124   CpvInitialize(int, CldLoadBalanceMessages);
125   CpvInitialize(int, CldMessageChunks);
126   CpvAccess(CldRelocatedMessages) = CpvAccess(CldLoadBalanceMessages) = 
127     CpvAccess(CldMessageChunks) = 0;
128   CldModuleGeneralInit();
129 }