00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #define _GNU_SOURCE
00014
00015 #include "converse.h"
00016 #include "sockRoutines.h"
00017
00018 #define DEBUGP(x)
00019 CpvDeclare(int, myCPUAffToCore);
00020 #if CMK_OS_IS_LINUX
00021
00022
00023
00024
00025
00026 CpvDeclare(void *, myProcStatFP);
00027 #endif
00028
00029 #if CMK_HAS_SETAFFINITY || defined (_WIN32) || CMK_HAS_BINDPROCESSOR
00030
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <unistd.h>
00034
00035 #ifdef _WIN32
00036 #include <windows.h>
00037 #include <winbase.h>
00038 #else
00039 #define _GNU_SOURCE
00040 #include <sched.h>
00041
00042
00043 #endif
00044
00045 #if CMK_OS_IS_LINUX
00046 #include <sys/syscall.h>
00047 #endif
00048
00049 #if defined(__APPLE__)
00050 #include <Carbon/Carbon.h>
00051 #endif
00052
00053 #if defined(ARCH_HPUX11) || defined(ARCH_HPUX10)
00054 #include <sys/mpctl.h>
00055 #endif
00056
00057
00058 #define MAX_EXCLUDE 64
00059 static int excludecore[MAX_EXCLUDE] = {-1};
00060 static int excludecount = 0;
00061
00062 static int affinity_doneflag = 0;
00063
00064 static int in_exclude(int core)
00065 {
00066 int i;
00067 for (i=0; i<excludecount; i++) if (core == excludecore[i]) return 1;
00068 return 0;
00069 }
00070
00071 static void add_exclude(int core)
00072 {
00073 if (in_exclude(core)) return;
00074 CmiAssert(excludecount < MAX_EXCLUDE);
00075 excludecore[excludecount++] = core;
00076 }
00077
00078 #if CMK_HAS_BINDPROCESSOR
00079 #include <sys/processor.h>
00080 #endif
00081
00082 #define SET_MASK(cpuid) \
00083 \
00084 if ((cpuid / 8) > len) { \
00085 printf("Mask size too small to handle requested CPU ID\n"); \
00086 return -1; \
00087 } else { \
00088 mask = 1 << cpuid; \
00089 }
00090
00091
00092
00093
00094
00095 int set_cpu_affinity(unsigned int cpuid) {
00096 unsigned long mask = 0xffffffff;
00097 unsigned int len = sizeof(mask);
00098 int retValue = 0;
00099 int pid;
00100
00101 #ifdef _WIN32
00102 HANDLE hProcess;
00103 #endif
00104
00105 #ifdef _WIN32
00106 SET_MASK(cpuid)
00107 hProcess = GetCurrentProcess();
00108 if (SetProcessAffinityMask(hProcess, mask) == 0) {
00109 return -1;
00110 }
00111 #elif CMK_HAS_BINDPROCESSOR
00112 pid = getpid();
00113 if (bindprocessor(BINDPROCESS, pid, cpuid) == -1) return -1;
00114 #else
00115 cpu_set_t cpuset;
00116 CPU_ZERO(&cpuset);
00117 CPU_SET(cpuid, &cpuset);
00118
00119
00120
00121
00122 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0) {
00123 perror("sched_setaffinity");
00124 return -1;
00125 }
00126 #endif
00127
00128 return 0;
00129 }
00130
00131 #if CMK_SMP
00132 int set_thread_affinity(int cpuid) {
00133 unsigned long mask = 0xffffffff;
00134 unsigned int len = sizeof(mask);
00135
00136 #ifdef _WIN32
00137 HANDLE hThread;
00138 #endif
00139
00140 #ifdef _WIN32
00141 SET_MASK(cpuid)
00142 hThread = GetCurrentThread();
00143 if (SetThreadAffinityMask(hThread, mask) == 0) {
00144 return -1;
00145 }
00146 #elif CMK_HAS_PTHREAD_SETAFFINITY
00147 int s, j;
00148 cpu_set_t cpuset;
00149 pthread_t thread;
00150
00151 thread = pthread_self();
00152
00153 CPU_ZERO(&cpuset);
00154 CPU_SET(cpuid, &cpuset);
00155
00156 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
00157 if (s != 0) {
00158 perror("pthread_setaffinity");
00159 return -1;
00160 }
00161 #elif CMK_HAS_BINDPROCESSOR
00162 if (bindprocessor(BINDTHREAD, thread_self(), cpuid) != 0)
00163 return -1;
00164 #else
00165 return set_cpu_affinity(cpuid);
00166 #endif
00167
00168 return 0;
00169 }
00170 #endif
00171
00172
00173 int CmiSetCPUAffinity(int mycore)
00174 {
00175 int core = mycore;
00176 if (core < 0) {
00177 core = CmiNumCores() + core;
00178 }
00179 if (core < 0) {
00180 CmiError("Error: Invalid cpu affinity core number: %d\n", mycore);
00181 CmiAbort("CmiSetCPUAffinity failed");
00182 }
00183
00184 CpvAccess(myCPUAffToCore) = core;
00185
00186
00187 #if CMK_SMP
00188 return set_thread_affinity(core);
00189 #else
00190 return set_cpu_affinity(core);
00191
00192 #endif
00193 }
00194
00195
00196
00197
00198 int print_cpu_affinity() {
00199 #ifdef _WIN32
00200 unsigned long pMask, sMask;
00201 HANDLE hProcess = GetCurrentProcess();
00202 if(GetProcessAffinityMask(hProcess, &pMask, &sMask)){
00203 perror("On Windows: GetProcessAffinityMask");
00204 return -1;
00205 }
00206
00207 CmiPrintf("[%d] CPU affinity mask is: 0x%08lx\n", CmiMyPe(), pMask);
00208
00209 #elif CMK_HAS_BINDPROCESSOR
00210 printf("[%d] CPU affinity mask is unknown for AIX. \n", CmiMyPe());
00211 #else
00212
00213
00214 cpu_set_t cpuset;
00215 char str[256], pe[16];
00216 int i;
00217 CPU_ZERO(&cpuset);
00218
00219
00220
00221 if (sched_getaffinity(0, sizeof(cpuset), &cpuset) < 0) {
00222 perror("sched_getaffinity");
00223 return -1;
00224 }
00225
00226 sprintf(str, "[%d] CPU affinity mask is: ", CmiMyPe());
00227 for (i = 0; i < CPU_SETSIZE; i++)
00228 if (CPU_ISSET(i, &cpuset)) {
00229 sprintf(pe, " %d ", i);
00230 strcat(str, pe);
00231 }
00232 CmiPrintf("%s\n", str);
00233 #endif
00234 return 0;
00235 }
00236
00237 #if CMK_SMP
00238 int print_thread_affinity() {
00239 unsigned long mask;
00240 size_t len = sizeof(mask);
00241
00242 #if CMK_HAS_PTHREAD_SETAFFINITY
00243 int s, j;
00244 cpu_set_t cpuset;
00245 pthread_t thread;
00246 char str[256], pe[16];
00247
00248 thread = pthread_self();
00249 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
00250 if (s != 0) {
00251 perror("pthread_getaffinity");
00252 return -1;
00253 }
00254
00255 sprintf(str, "[%d] %s affinity is: ", CmiMyPe(), CmiMyPe()>=CmiNumPes()?"communication pthread":"pthread");
00256 for (j = 0; j < CPU_SETSIZE; j++)
00257 if (CPU_ISSET(j, &cpuset)) {
00258 sprintf(pe, " %d ", j);
00259 strcat(str, pe);
00260 }
00261 CmiPrintf("%s\n", str);
00262 #endif
00263 return 0;
00264 }
00265 #endif
00266
00267 int CmiPrintCPUAffinity()
00268 {
00269 #if CMK_SMP
00270 return print_thread_affinity();
00271 #else
00272 return print_cpu_affinity();
00273 #endif
00274 }
00275
00276 int CmiOnCore() {
00277 #if CMK_OS_IS_LINUX
00278
00279
00280
00281
00282
00283
00284
00285
00286 #define TASK_CPU_POS (39)
00287 int n;
00288 char str[128];
00289 FILE *fp = (FILE *)CpvAccess(myProcStatFP);
00290 if (fp == NULL){
00291 printf("WARNING: CmiOnCore IS NOT SUPPORTED ON THIS PLATFORM\n");
00292 return -1;
00293 }
00294 fseek(fp, 0, SEEK_SET);
00295 for (n=0; n<TASK_CPU_POS; n++) {
00296 fscanf(fp, "%s", str);
00297 }
00298 return atoi(str);
00299 #else
00300 printf("WARNING: CmiOnCore IS NOT SUPPORTED ON THIS PLATFORM\n");
00301 return -1;
00302 #endif
00303 }
00304
00305
00306 static int cpuAffinityHandlerIdx;
00307 static int cpuAffinityRecvHandlerIdx;
00308
00309 typedef struct _hostnameMsg {
00310 char core[CmiMsgHeaderSizeBytes];
00311 int pe;
00312 skt_ip_t ip;
00313 int ncores;
00314 int rank;
00315 int seq;
00316 } hostnameMsg;
00317
00318 typedef struct _rankMsg {
00319 char core[CmiMsgHeaderSizeBytes];
00320 int *ranks;
00321 int *nodes;
00322 } rankMsg;
00323
00324 static rankMsg *rankmsg = NULL;
00325 static CmmTable hostTable;
00326 static CmiNodeLock affLock = 0;
00327
00328
00329 static void cpuAffinityHandler(void *m)
00330 {
00331 static int count = 0;
00332 static int nodecount = 0;
00333 hostnameMsg *rec;
00334 hostnameMsg *msg = (hostnameMsg *)m;
00335 hostnameMsg *tmpm;
00336 int tag, tag1, pe, myrank;
00337 int npes = CmiNumPes();
00338
00339
00340
00341
00342
00343
00344 CmiAssert(CmiMyPe()==0 && rankmsg != NULL);
00345 tag = *(int*)&msg->ip;
00346 pe = msg->pe;
00347 if ((rec = (hostnameMsg *)CmmProbe(hostTable, 1, &tag, &tag1)) != NULL) {
00348 CmiFree(msg);
00349 }
00350 else {
00351 rec = msg;
00352 rec->seq = nodecount;
00353 nodecount++;
00354 CmmPut(hostTable, 1, &tag, msg);
00355 }
00356 myrank = rec->rank%rec->ncores;
00357 while (in_exclude(myrank)) {
00358 myrank = (myrank+1)%rec->ncores;
00359 rec->rank ++;
00360 }
00361 rankmsg->ranks[pe] = myrank;
00362 rankmsg->nodes[pe] = rec->seq;
00363 rec->rank ++;
00364 count ++;
00365 if (count == CmiNumPes()) {
00366
00367 tag = CmmWildCard;
00368 while (tmpm = CmmGet(hostTable, 1, &tag, &tag1)) CmiFree(tmpm);
00369 CmmFree(hostTable);
00370 #if 1
00371
00372 {
00373 int i,j;
00374 for (i=0; i<npes-1; i++)
00375 for(j=i+1; j<npes; j++) {
00376 if (rankmsg->nodes[i] == rankmsg->nodes[j] &&
00377 rankmsg->ranks[i] > rankmsg->ranks[j])
00378 {
00379 int tmp = rankmsg->ranks[i];
00380 rankmsg->ranks[i] = rankmsg->ranks[j];
00381 rankmsg->ranks[j] = tmp;
00382 }
00383 }
00384 }
00385 #endif
00386 CmiSyncBroadcastAllAndFree(sizeof(rankMsg)+CmiNumPes()*sizeof(int)*2, (void *)rankmsg);
00387 }
00388 }
00389
00390
00391 static void cpuAffinityRecvHandler(void *msg)
00392 {
00393 int myrank, mynode;
00394 rankMsg *m = (rankMsg *)msg;
00395 m->ranks = (int *)((char*)m + sizeof(rankMsg));
00396 m->nodes = (int *)((char*)m + sizeof(rankMsg) + CmiNumPes()*sizeof(int));
00397 myrank = m->ranks[CmiMyPe()];
00398 mynode = m->nodes[CmiMyPe()];
00399
00400
00401
00402 if (-1 != CmiSetCPUAffinity(myrank)) {
00403 DEBUGP(("Processor %d is bound to core #%d on node #%d\n", CmiMyPe(), myrank, mynode));
00404 }
00405 else{
00406 CmiPrintf("Processor %d set affinity failed!\n", CmiMyPe());
00407 CmiAbort("set cpu affinity abort!\n");
00408 }
00409 CmiFree(m);
00410 }
00411
00412 #if defined(_WIN32) && ! defined(__CYGWIN__)
00413
00414 #define strtok_r(x,y,z) strtok(x,y)
00415 #endif
00416
00417 static int search_pemap(char *pecoremap, int pe)
00418 {
00419 int *map = (int *)malloc(CmiNumPes()*sizeof(int));
00420 char *ptr = NULL;
00421 int i, j, k, count;
00422 char *str;
00423
00424 char *mapstr = (char*)malloc(strlen(pecoremap)+1);
00425 strcpy(mapstr, pecoremap);
00426
00427 str = strtok_r(mapstr, ",", &ptr);
00428 count = 0;
00429 while (str && count < CmiNumPes())
00430 {
00431 int hasdash=0, hascolon=0, hasdot=0, hasstar1=0, hasstar2 = 0;
00432 int start, end, stride=1, block=1;
00433 int iter=1;
00434 for (i=0; i<strlen(str); i++) {
00435 if (str[i] == '-' && i!=0) hasdash=1;
00436 else if (str[i] == ':') hascolon=1;
00437 else if (str[i] == '.') hasdot=1;
00438 else if (str[i] == 'x') hasstar1=1;
00439 else if (str[i] == 'X') hasstar2=1;
00440 }
00441 if (hasstar1 || hasstar2) {
00442 if (hasstar1) sscanf(str, "%dx", &iter);
00443 if (hasstar2) sscanf(str, "%dX", &iter);
00444 while (*str!='x' && *str!='X') str++;
00445 str++;
00446 }
00447 if (hasdash) {
00448 if (hascolon) {
00449 if (hasdot) {
00450 if (sscanf(str, "%d-%d:%d.%d", &start, &end, &stride, &block) != 4)
00451 printf("Warning: Check the format of \"%s\".\n", str);
00452 }
00453 else {
00454 if (sscanf(str, "%d-%d:%d", &start, &end, &stride) != 3)
00455 printf("Warning: Check the format of \"%s\".\n", str);
00456 }
00457 }
00458 else {
00459 if (sscanf(str, "%d-%d", &start, &end) != 2)
00460 printf("Warning: Check the format of \"%s\".\n", str);
00461 }
00462 }
00463 else {
00464 sscanf(str, "%d", &start);
00465 end = start;
00466 }
00467 if (block > stride) {
00468 printf("Warning: invalid block size in \"%s\" ignored.\n", str);
00469 block=1;
00470 }
00471
00472 for (k = 0; k<iter; k++) {
00473 for (i = start; i<=end; i+=stride) {
00474 for (j=0; j<block; j++) {
00475 if (i+j>end) break;
00476 map[count++] = i+j;
00477 if (count == CmiNumPes()) break;
00478 }
00479 if (count == CmiNumPes()) break;
00480 }
00481 if (count == CmiNumPes()) break;
00482 }
00483 str = strtok_r(NULL, ",", &ptr);
00484 }
00485 i = map[pe % count];
00486
00487 free(map);
00488 free(mapstr);
00489 return i;
00490 }
00491
00492 #if CMK_CRAYXT || CMK_CRAYXE
00493 extern int getXTNodeID(int mpirank, int nummpiranks);
00494 #endif
00495
00496
00497 void CmiInitCPUAffinity(char **argv)
00498 {
00499 static skt_ip_t myip;
00500 int ret, i, exclude;
00501 hostnameMsg *msg;
00502 char *pemap = NULL;
00503 char *commap = NULL;
00504 char *pemapfile = NULL;
00505
00506 int show_affinity_flag;
00507 int affinity_flag = CmiGetArgFlagDesc(argv,"+setcpuaffinity",
00508 "set cpu affinity");
00509
00510 while (CmiGetArgIntDesc(argv,"+excludecore", &exclude, "avoid core when setting cpuaffinity")) {
00511 if (CmiMyRank() == 0) add_exclude(exclude);
00512 affinity_flag = 1;
00513 }
00514
00515 if (CmiGetArgStringDesc(argv, "+pemapfile", &pemapfile, "define pe to core mapping file")) {
00516 FILE *fp;
00517 char buf[128];
00518 pemap = (char*)malloc(1024);
00519 fp = fopen(pemapfile, "r");
00520 if (fp == NULL) CmiAbort("pemapfile does not exist");
00521 while (!feof(fp)) {
00522 if (fgets(buf, 128, fp)) {
00523 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
00524 strcat(pemap, buf);
00525 }
00526 }
00527 if (CmiMyPe()==0) CmiPrintf("Charm++> read from pemap file '%s': %s\n", pemapfile, pemap);
00528 }
00529
00530 CmiGetArgStringDesc(argv, "+pemap", &pemap, "define pe to core mapping");
00531 if (pemap!=NULL && excludecount>0)
00532 CmiAbort("Charm++> +pemap can not be used with +excludecore.\n");
00533
00534 CmiGetArgStringDesc(argv, "+commap", &commap, "define comm threads to core mapping");
00535
00536 if (pemap!=NULL || commap!=NULL) affinity_flag = 1;
00537
00538 show_affinity_flag = CmiGetArgFlagDesc(argv,"+showcpuaffinity",
00539 "print cpu affinity");
00540
00541 cpuAffinityHandlerIdx =
00542 CmiRegisterHandler((CmiHandler)cpuAffinityHandler);
00543 cpuAffinityRecvHandlerIdx =
00544 CmiRegisterHandler((CmiHandler)cpuAffinityRecvHandler);
00545
00546 if (CmiMyRank() ==0) {
00547 affLock = CmiCreateLock();
00548 }
00549
00550 #if CMK_BLUEGENEP | CMK_BLUEGENEQ
00551 if(affinity_flag){
00552 affinity_flag = 0;
00553 if(CmiMyPe()==0) CmiPrintf("Charm++> cpu affinity setting is not needed on BG/P, thus ignored.\n");
00554 }
00555 if(show_affinity_flag){
00556 show_affinity_flag = 0;
00557 if(CmiMyPe()==0) CmiPrintf("Charm++> printing cpu affinity is not supported on BG/P.\n");
00558 }
00559 #endif
00560
00561 if (!affinity_flag) {
00562 if (show_affinity_flag) CmiPrintCPUAffinity();
00563 return;
00564 }
00565
00566 if (CmiMyPe() == 0) {
00567 CmiPrintf("Charm++> cpu affinity enabled. \n");
00568 if (excludecount > 0) {
00569 CmiPrintf("Charm++> cpuaffinity excludes core: %d", excludecore[0]);
00570 for (i=1; i<excludecount; i++) CmiPrintf(" %d", excludecore[i]);
00571 CmiPrintf(".\n");
00572 }
00573 if (pemap!=NULL)
00574 CmiPrintf("Charm++> cpuaffinity PE-core map : %s\n", pemap);
00575 }
00576
00577 if (CmiMyPe() >= CmiNumPes()) {
00578
00579
00580 CmiNodeAllBarrier();
00581 if (commap != NULL) {
00582 int mycore = search_pemap(commap, CmiMyPe()-CmiNumPes());
00583 if(CmiMyPe()-CmiNumPes()==0) printf("Charm++> set comm %d on node %d to core #%d\n", CmiMyPe()-CmiNumPes(), CmiMyNode(), mycore);
00584 if (-1 == CmiSetCPUAffinity(mycore))
00585 CmiAbort("set_cpu_affinity abort!");
00586 CmiNodeAllBarrier();
00587 if (show_affinity_flag) CmiPrintCPUAffinity();
00588 return;
00589 }
00590 else {
00591
00592 #if !CMK_CRAYXT && !CMK_CRAYXE && !CMK_BLUEGENEQ
00593 if (pemap == NULL) {
00594 #if CMK_MACHINE_PROGRESS_DEFINED
00595 while (affinity_doneflag < CmiMyNodeSize()) CmiNetworkProgress();
00596 #else
00597 #if CMK_SMP
00598 #error "Machine progress call needs to be implemented for cpu affinity!"
00599 #endif
00600 #endif
00601 }
00602 #endif
00603 #if CMK_CRAYXT || CMK_CRAYXE
00604
00605 if (pemap != NULL)
00606 #endif
00607 {
00608 CmiNodeAllBarrier();
00609 if (show_affinity_flag) CmiPrintCPUAffinity();
00610 return;
00611 }
00612 }
00613 }
00614
00615 if (pemap != NULL && CmiMyPe()<CmiNumPes()) {
00616 int mycore = search_pemap(pemap, CmiMyPe());
00617 if(show_affinity_flag) CmiPrintf("Charm++> set PE %d on node %d to core #%d\n", CmiMyPe(), CmiMyNode(), mycore);
00618 if (mycore >= CmiNumCores()) {
00619 CmiPrintf("Error> Invalid core number %d, only have %d cores (0-%d) on the node. \n", mycore, CmiNumCores(), CmiNumCores()-1);
00620 CmiAbort("Invalid core number");
00621 }
00622 if (CmiSetCPUAffinity(mycore) == -1) CmiAbort("set_cpu_affinity abort!");
00623 CmiNodeAllBarrier();
00624 CmiNodeAllBarrier();
00625
00626 return;
00627 }
00628
00629 #if CMK_CRAYXT || CMK_CRAYXE
00630 {
00631 int numPes = CmiNumPes();
00632 int numNodes = CmiNumNodes();
00633 int numCores = CmiNumCores();
00634
00635 int myid = getXTNodeID(CmiMyNode(), CmiNumNodes());
00636 int myrank;
00637 int pe, mype = CmiMyPe();
00638 int node = CmiMyNode();
00639 int nnodes = 0;
00640 #if CMK_SMP
00641 if (CmiMyPe() >= CmiNumPes()) {
00642 int node = CmiMyPe() - CmiNumPes();
00643 mype = CmiNodeFirst(node) + CmiMyNodeSize() - 1;
00644 }
00645 #endif
00646 pe = mype - 1;
00647 while (pe >= 0) {
00648 int n = CmiNodeOf(pe);
00649 if (n != node) { nnodes++; node = n; }
00650 if (getXTNodeID(n, numNodes) != myid) break;
00651 pe --;
00652 }
00653 CmiAssert(numCores > 0);
00654 myrank = (mype - pe - 1 + nnodes)%numCores;
00655 #if CMK_SMP
00656 if (CmiMyPe() >= CmiNumPes())
00657 myrank = (myrank + 1)%numCores;
00658 #endif
00659
00660 if (-1 != CmiSetCPUAffinity(myrank)) {
00661 DEBUGP(("Processor %d is bound to core #%d on node #%d\n", CmiMyPe(), myrank, mynode));
00662 }
00663 else{
00664 CmiPrintf("Processor %d set affinity failed!\n", CmiMyPe());
00665 CmiAbort("set cpu affinity abort!\n");
00666 }
00667 }
00668 if (CmiMyPe() < CmiNumPes())
00669 CmiNodeAllBarrier();
00670 CmiNodeAllBarrier();
00671 #else
00672
00673 if (CmiMyRank() == 0)
00674 {
00675 #if CMK_HAS_GETHOSTNAME
00676 myip = skt_my_ip();
00677 #else
00678 CmiAbort("Can not get unique name for the compute nodes. \n");
00679 #endif
00680 }
00681 CmiNodeAllBarrier();
00682
00683
00684 msg = (hostnameMsg *)CmiAlloc(sizeof(hostnameMsg));
00685 CmiSetHandler((char *)msg, cpuAffinityHandlerIdx);
00686 msg->pe = CmiMyPe();
00687 msg->ip = myip;
00688 msg->ncores = CmiNumCores();
00689 DEBUGP(("PE %d's node has %d number of cores. \n", CmiMyPe(), msg->ncores));
00690 msg->rank = 0;
00691 CmiSyncSendAndFree(0, sizeof(hostnameMsg), (void *)msg);
00692
00693 if (CmiMyPe() == 0) {
00694 int i;
00695 hostTable = CmmNew();
00696 rankmsg = (rankMsg *)CmiAlloc(sizeof(rankMsg)+CmiNumPes()*sizeof(int)*2);
00697 CmiSetHandler((char *)rankmsg, cpuAffinityRecvHandlerIdx);
00698 rankmsg->ranks = (int *)((char*)rankmsg + sizeof(rankMsg));
00699 rankmsg->nodes = (int *)((char*)rankmsg + sizeof(rankMsg) + CmiNumPes()*sizeof(int));
00700 for (i=0; i<CmiNumPes(); i++) {
00701 rankmsg->ranks[i] = 0;
00702 rankmsg->nodes[i] = -1;
00703 }
00704
00705 for (i=0; i<CmiNumPes(); i++) CmiDeliverSpecificMsg(cpuAffinityHandlerIdx);
00706 }
00707
00708
00709 CmiDeliverSpecificMsg(cpuAffinityRecvHandlerIdx);
00710 CmiLock(affLock);
00711 affinity_doneflag++;
00712 CmiUnlock(affLock);
00713 CmiNodeAllBarrier();
00714 #endif
00715
00716 if (show_affinity_flag) CmiPrintCPUAffinity();
00717 }
00718
00719
00720 void CmiInitCPUAffinityUtil(){
00721 CpvInitialize(int, myCPUAffToCore);
00722 CpvAccess(myCPUAffToCore) = -1;
00723 #if CMK_OS_IS_LINUX
00724 CpvInitialize(void *, myProcStatFP);
00725 char fname[64];
00726 CmiLock(_smp_mutex);
00727 #if CMK_SMP
00728 sprintf(fname, "/proc/%d/task/%d/stat", getpid(), syscall(SYS_gettid));
00729 #else
00730 sprintf(fname, "/proc/%d/stat", getpid());
00731 #endif
00732 CpvAccess(myProcStatFP) = (void *)fopen(fname, "r");
00733 CmiUnlock(_smp_mutex);
00734
00735
00736
00737
00738
00739 #endif
00740 }
00741
00742 #else
00743
00744
00745 int CmiPrintCPUAffinity()
00746 {
00747 CmiPrintf("Warning: CmiPrintCPUAffinity not supported.\n");
00748 }
00749
00750 void CmiInitCPUAffinity(char **argv)
00751 {
00752 char *pemap = NULL;
00753 char *pemapfile = NULL;
00754 char *commap = NULL;
00755 int excludecore = -1;
00756 int affinity_flag = CmiGetArgFlagDesc(argv,"+setcpuaffinity",
00757 "set cpu affinity");
00758 while (CmiGetArgIntDesc(argv,"+excludecore",&excludecore, "avoid core when setting cpuaffinity"));
00759 CmiGetArgStringDesc(argv, "+pemap", &pemap, "define pe to core mapping");
00760 CmiGetArgStringDesc(argv, "+pemapfile", &pemapfile, "define pe to core mapping file");
00761 CmiGetArgStringDesc(argv, "+commap", &commap, "define comm threads to core mapping");
00762 if (affinity_flag && CmiMyPe()==0)
00763 CmiPrintf("sched_setaffinity() is not supported, +setcpuaffinity disabled.\n");
00764 }
00765
00766
00767 void CmiInitCPUAffinityUtil(){
00768 CpvInitialize(int, myCPUAffToCore);
00769 CpvAccess(myCPUAffToCore) = -1;
00770 #if CMK_OS_IS_LINUX
00771 CpvInitialize(void *, myProcStatFP);
00772 CpvAccess(myProcStatFP) = NULL;
00773 #endif
00774 }
00775
00776 int CmiOnCore(){
00777 printf("WARNING: CmiOnCore IS NOT SUPPORTED ON THIS PLATFORM\n");
00778 return -1;
00779 }
00780 #endif