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