00001 #include "converse.h"
00002 #include "cldb.h"
00003 #include "queueing.h"
00004 #include <stdlib.h>
00005
00006 void LoadNotifyFn(int l)
00007 {
00008 }
00009
00010 const char *CldGetStrategy(void)
00011 {
00012 return "rand";
00013 }
00014
00015 void CldBalanceHandler(void *msg)
00016 {
00017 CldRestoreHandler((char *)msg);
00018 CldPutToken((char *)msg);
00019 }
00020
00021 void CldHandler(char *msg)
00022 {
00023 int len, queueing, priobits;
00024 unsigned int *prioptr;
00025 CldInfoFn ifn; CldPackFn pfn;
00026
00027 CldRestoreHandler(msg);
00028 ifn = (CldInfoFn)CmiHandlerToFunction(CmiGetInfo(msg));
00029 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00030 CsdEnqueueGeneral(msg, queueing, priobits, prioptr);
00031 }
00032
00033 void CldEnqueueMulti(int npes, const int *pes, void *msg, int infofn)
00034 {
00035 int len, queueing, priobits,i; unsigned int *prioptr;
00036 CldInfoFn ifn = (CldInfoFn)CmiHandlerToFunction(infofn);
00037 CldPackFn pfn;
00038 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00039 if (pfn) {
00040 pfn(&msg);
00041 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00042 }
00043 CldSwitchHandler((char *)msg, CpvAccess(CldHandlerIndex));
00044 CmiSetInfo(msg,infofn);
00045 for(i=0;i<npes;i++) {
00046 CmiSyncSend(pes[i], len, msg);
00047 }
00048 CmiFree(msg);
00049 }
00050
00051 void CldEnqueue(int pe, void *msg, int infofn)
00052 {
00053 int len, queueing, priobits; unsigned int *prioptr;
00054 CldInfoFn ifn = (CldInfoFn)CmiHandlerToFunction(infofn);
00055 CldPackFn pfn;
00056
00057 if (pe == CLD_ANYWHERE) {
00058 pe = (((CrnRand()+CmiMyPe())&0x7FFFFFFF)%CmiNumPes());
00059 while (!CldPresentPE(pe))
00060 pe = (((CrnRand()+CmiMyPe())&0x7FFFFFFF)%CmiNumPes());
00061 if (pe != CmiMyPe())
00062 CpvAccess(CldRelocatedMessages)++;
00063 if (pe == CmiMyPe()) {
00064 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00065 CmiSetInfo(msg,infofn);
00066 CldPutToken((char *)msg);
00067 }
00068 else {
00069 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00070 if (pfn) {
00071 pfn(&msg);
00072 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00073 }
00074 CldSwitchHandler((char *)msg, CpvAccess(CldBalanceHandlerIndex));
00075 CmiSetInfo(msg,infofn);
00076 CmiSyncSendAndFree(pe, len, msg);
00077 }
00078 }
00079 else if ((pe == CmiMyPe()) || (CmiNumPes() == 1)) {
00080 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00081 CmiSetInfo(msg,infofn);
00082 CsdEnqueueGeneral(msg, CQS_QUEUEING_LIFO, priobits, prioptr);
00083 }
00084 else {
00085 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00086 if (pfn) {
00087 pfn(&msg);
00088 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00089 }
00090 CldSwitchHandler((char *)msg, CpvAccess(CldHandlerIndex));
00091 CmiSetInfo(msg,infofn);
00092 if (pe==CLD_BROADCAST) { CmiSyncBroadcastAndFree(len, msg); }
00093 else if (pe==CLD_BROADCAST_ALL) { CmiSyncBroadcastAllAndFree(len, msg); }
00094 else CmiSyncSendAndFree(pe, len, msg);
00095 }
00096 }
00097
00098 void CldNodeEnqueue(int node, void *msg, int infofn)
00099 {
00100 int len, queueing, priobits; unsigned int *prioptr;
00101 CldInfoFn ifn = (CldInfoFn)CmiHandlerToFunction(infofn);
00102 CldPackFn pfn;
00103 if (node == CLD_ANYWHERE) {
00104
00105 node = (((CrnRand()+CmiMyNode())&0x7FFFFFFF)%CmiNumNodes());
00106 if (node != CmiMyNode())
00107 CpvAccess(CldRelocatedMessages)++;
00108 }
00109 if (node == CmiMyNode()) {
00110 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00111 CsdNodeEnqueueGeneral(msg, queueing, priobits, prioptr);
00112 }
00113 else {
00114 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00115 if (pfn) {
00116 pfn(&msg);
00117 ifn(msg, &pfn, &len, &queueing, &priobits, &prioptr);
00118 }
00119 CldSwitchHandler((char *)msg, CpvAccess(CldHandlerIndex));
00120 CmiSetInfo(msg,infofn);
00121 if (node==CLD_BROADCAST) { CmiSyncNodeBroadcastAndFree(len, msg); }
00122 else if (node==CLD_BROADCAST_ALL){CmiSyncNodeBroadcastAllAndFree(len,msg);}
00123 else CmiSyncNodeSendAndFree(node, len, msg);
00124 }
00125 }
00126
00127 void CldModuleInit(char **argv)
00128 {
00129 CpvInitialize(int, CldHandlerIndex);
00130 CpvInitialize(int, CldBalanceHandlerIndex);
00131 CpvAccess(CldHandlerIndex) = CmiRegisterHandler((CmiHandler)CldHandler);
00132 CpvAccess(CldBalanceHandlerIndex) = CmiRegisterHandler(CldBalanceHandler);
00133 CpvInitialize(int, CldRelocatedMessages);
00134 CpvInitialize(int, CldLoadBalanceMessages);
00135 CpvInitialize(int, CldMessageChunks);
00136 CpvAccess(CldRelocatedMessages) = CpvAccess(CldLoadBalanceMessages) =
00137 CpvAccess(CldMessageChunks) = 0;
00138 CldModuleGeneralInit(argv);
00139 }