tcharm initization sends a function pointer in the tcharmInitMsg to broadcast to...
authorGengbin Zheng <gzheng@illinois.edu>
Fri, 6 Feb 2009 06:51:29 +0000 (06:51 +0000)
committerGengbin Zheng <gzheng@illinois.edu>
Fri, 6 Feb 2009 06:51:29 +0000 (06:51 +0000)
Change it to a function registration scheme by introducing a function TCHARM_Register_thread_function() to register a tcharm thread start function to an integer handler.

src/libs/ck-libs/tcharm/tcharm.C
src/libs/ck-libs/tcharm/tcharm.h
src/libs/ck-libs/tcharm/tcharm_impl.h
src/libs/ck-libs/tcharm/tcharmc.h

index ae23b303ca4d736c37b1640d48f0057098799fa6..bd006151a29b919f66c3cfed50ab31efd9a2ac07 100644 (file)
@@ -110,20 +110,31 @@ void TCHARM_Api_trace(const char *routineName,const char *libraryName)
        CmiPrintf("\n");
 }
 
-#if CMK_TCHARM_FNPTR_HACK
-CDECL void AMPI_threadstart(void *data);
-#endif
+// register thread start functions to get a function handler
+// this is portable across heterogeneous platforms, or on machines with
+// random stack/function pointer
+
+static CkVec<TCHARM_Thread_data_start_fn> threadFnTable;
+
+int TCHARM_Register_thread_function(TCHARM_Thread_data_start_fn fn)
+{
+  int idx = threadFnTable.size();
+  threadFnTable.push_back(fn);
+  return idx+1;                     // make 0 invalid number
+}
+
+TCHARM_Thread_data_start_fn getTCharmThreadFunction(int idx)
+{
+  CmiAssert(idx > 0);
+  return threadFnTable[idx-1];
+}
 
 static void startTCharmThread(TCharmInitMsg *msg)
 {
        DBGX("thread started");
        TCharm::activateThread();
-       typedef void (*threadFn_t)(void *);
-#if CMK_TCHARM_FNPTR_HACK
-       ((threadFn_t)AMPI_threadstart)(msg->data);
-#else
-       ((threadFn_t)msg->threadFn)(msg->data);
-#endif
+        TCHARM_Thread_data_start_fn threadFn = getTCharmThreadFunction(msg->threadFn);
+       threadFn(msg->data);
        TCharm::deactivateThread();
        CtvAccess(_curTCharm)->done();
 }
@@ -618,26 +629,26 @@ CDECL void TCHARM_Set_exit(void) { g_tcharmOptions.exitWhenDone=1; }
 
 /*Create a new array of threads, which will be bound to by subsequent libraries*/
 CDECL void TCHARM_Create(int nThreads,
-                       TCHARM_Thread_start_fn threadFn)
+                       int threadFn)
 {
        TCHARMAPI("TCHARM_Create");
        TCHARM_Create_data(nThreads,
-                        (TCHARM_Thread_data_start_fn)threadFn,NULL,0);
+                        threadFn,NULL,0);
 }
 FDECL void FTN_NAME(TCHARM_CREATE,tcharm_create)
-       (int *nThreads,TCHARM_Thread_start_fn threadFn)
+       (int *nThreads,int threadFn)
 { TCHARM_Create(*nThreads,threadFn); }
 
 static CProxy_TCharm TCHARM_Build_threads(TCharmInitMsg *msg);
 
 /*As above, but pass along (arbitrary) data to threads*/
 CDECL void TCHARM_Create_data(int nThreads,
-                 TCHARM_Thread_data_start_fn threadFn,
+                 int threadFn,
                  void *threadData,int threadDataLen)
 {
        TCHARMAPI("TCHARM_Create_data");
        TCharmInitMsg *msg=new (threadDataLen,0) TCharmInitMsg(
-               (CthVoidFn)threadFn,g_tcharmOptions);
+               threadFn,g_tcharmOptions);
        msg->numElements=nThreads;
        memcpy(msg->data,threadData,threadDataLen);
        TCHARM_Build_threads(msg);
@@ -648,7 +659,7 @@ CDECL void TCHARM_Create_data(int nThreads,
 
 FDECL void FTN_NAME(TCHARM_CREATE_DATA,tcharm_create_data)
        (int *nThreads,
-                 TCHARM_Thread_data_start_fn threadFn,
+                 int threadFn,
                  void *threadData,int *threadDataLen)
 { TCHARM_Create_data(*nThreads,threadFn,threadData,*threadDataLen); }
 
index d7b42d31ecb01d35162249704baa87850786f0cb..d9dab5294606e367d2bece8539aa5cea07addd60 100644 (file)
@@ -33,6 +33,8 @@ void TCHARM_Set_fallback_setup(TCHARM_Fallback_setup_fn f);
 
 #include "tcharm_impl.h"
 
+int TCHARM_Register_thread_function(TCHARM_Thread_data_start_fn fn);
+
 /*
 This "start" call finds the currently running set of tcharm threads,
 copies their arrayID into retTCharmArray, and returns an 
index 6c0a660eb0de77b86c7bd31b156534042695091a..b5034077048ab8c195edb3936be607ea57a24bc8 100644 (file)
@@ -49,7 +49,8 @@ public:
 class TCharmInitMsg : public CMessage_TCharmInitMsg {
  public:
        //Function to start thread with:
-       CthVoidFn threadFn;
+       //CthVoidFn threadFn;
+       int threadFn;
        //Initial thread parameters:
        TCHARM_Thread_options opts;
        //Array size (number of elements)
@@ -57,7 +58,7 @@ class TCharmInitMsg : public CMessage_TCharmInitMsg {
        //Data to pass to thread:
        char *data;
 
-       TCharmInitMsg(CthVoidFn threadFn_,const TCHARM_Thread_options &opts_)
+       TCharmInitMsg(int threadFn_,const TCHARM_Thread_options &opts_)
                :threadFn(threadFn_), opts(opts_) {}
 };
 
index 86dc78b513f4ced67c2c79510f869fa3bc2e84b0..becaa572ac46970190fae0085f1a277c82fede2c 100644 (file)
@@ -31,11 +31,11 @@ int TCHARM_Get_num_chunks(void);
 
 /*Create a new array of threads, which will be bound to by subsequent libraries*/
 typedef void (*TCHARM_Thread_start_fn)(void);
-void TCHARM_Create(int nThreads,TCHARM_Thread_start_fn threadFn);
+void TCHARM_Create(int nThreads,int threadFn);
 
 /*As above, but pass along (arbitrary) data to thread*/
 typedef void (*TCHARM_Thread_data_start_fn)(void *threadData);
-void TCHARM_Create_data(int nThreads,TCHARM_Thread_data_start_fn threadFn,
+void TCHARM_Create_data(int nThreads,int threadFn,
                  void *threadData,int threadDataLen);