00001 #ifndef CKLAMBDA_H
00002 #define CKLAMBDA_H
00003
00004 #include <functional>
00005
00006 #if CMK_SMP && USE_CKLOOP
00007
00008 #include "CkLoopAPI.h"
00009
00010 static void CkLoop_LambdaHelperFn(int first, int last, void *result, int paramNum, void *param) {
00011 (*static_cast<std::function<void(int,int,void*)>*>(param))(first,last,result);
00012 }
00013
00014 static void CkLoop_LambdaCallerFn(int paramNum, void *param) {
00015 (*static_cast<std::function<void()>*>(param))();
00016 }
00017
00018
00019
00020
00021 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange,
00022 std::function<void(int,int,void*)> func
00023 ) {
00024 if ( numChunks < 2 || lowerRange == upperRange ) {
00025 func(lowerRange, upperRange, NULL);
00026 } else {
00027 CkLoop_Parallelize(CkLoop_LambdaHelperFn, 1, (void *)&func,
00028 numChunks, lowerRange, upperRange);
00029 }
00030 }
00031
00032 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange,
00033 std::function<void(int,int,void*)> func,
00034
00035 void *redResult=NULL, REDUCTION_TYPE type=CKLOOP_NONE,
00036 std::function<void()> cfunc=NULL
00037 ) {
00038
00039 if ( numChunks < 1 ) {
00040 if ( cfunc ) cfunc();
00041 func(lowerRange, upperRange, redResult);
00042 } else {
00043 if ( cfunc )
00044 CkLoop_Parallelize(CkLoop_LambdaHelperFn, 1, (void *)&func,
00045 numChunks, lowerRange, upperRange, 1, redResult, type,
00046 CkLoop_LambdaCallerFn, 1, (void *)&cfunc);
00047 else
00048 CkLoop_Parallelize(CkLoop_LambdaHelperFn, 1, (void *)&func,
00049 numChunks, lowerRange, upperRange, 1, redResult, type);
00050 }
00051 }
00052
00053 #else // CMK_SMP && USE_CKLOOP
00054
00055 template< class F >
00056 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange, F func ) {
00057 func(lowerRange, upperRange);
00058 }
00059
00060 template< class F, class C >
00061 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange, F func, C cfunc ) {
00062 cfunc();
00063 func(lowerRange, upperRange);
00064 }
00065
00066 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange,
00067 std::function<void(int,int)> func
00068 ) {
00069 func(lowerRange, upperRange);
00070 }
00071
00072 inline void CkLoop_Parallelize( int numChunks, int lowerRange, int upperRange,
00073 std::function<void(int,int)> func,
00074 std::function<void()> cfunc=nullptr
00075 ) {
00076 if ( cfunc ) cfunc();
00077 func(lowerRange, upperRange);
00078 }
00079
00080 #endif // CMK_SMP && USE_CKLOOP
00081
00082 #endif // CKLAMBDA_H