void printlocation() { #if defined(C_OPENSHMEM_PRACTICAL) || defined(F_OPENSHMEM_PRACTICAL) MPI_Init(NULL, NULL); #endif /* C_OPENSHMEM_PRACTICAL || F_OPENSHMEM_PRACTICAL */ #if defined(C_HYBRID_PRACTICAL) || defined(F_HYBRID_PRACTICAL) || defined(C_MPI_PRACTICAL) || defined(F_MPI_PRACTICAL) || defined(C_OPENSHMEM_PRACTICAL) || defined(F_OPENSHMEM_PRACTICAL) int rank, namelen; char hnbuf[MPI_MAX_PROCESSOR_NAME]; MPI_Comm_rank(MPI_COMM_WORLD, &rank); memset(hnbuf, 0, sizeof(hnbuf)); MPI_Get_processor_name(hnbuf, &namelen); #endif /* C_HYBRID_PRACTICAL || F_HYBRID_PRACTICAL || C_MPI_PRACTICAL || F_MPI_PRACTICAL || C_OPENSHMEM_PRACTICAL || F_OPENSHMEM_PRACTICAL */ #if defined(C_OPENMP_PRACTICAL) || defined(F_OPENMP_PRACTICAL) || defined(C_HYBRID_PRACTICAL) || defined(F_HYBRID_PRACTICAL) int thread; thread = omp_get_thread_num(); #endif /* C_OPENMP_PRACTICAL || F_OPENMP_PRACTICAL || C_HYBRID_PRACTICAL || F_HYBRID_PRACTICAL */ cpu_set_t coremask; char clbuf[7 * CPU_SETSIZE]; memset(clbuf, 0, sizeof(clbuf)); (void)sched_getaffinity(0, sizeof(coremask), &coremask); cpuset_to_cstr(&coremask, clbuf); #if defined(C_HYBRID_PRACTICAL) || defined(F_HYBRID_PRACTICAL) printf("Rank %d / thread %d on core %s of node <%s>\n", rank, thread, clbuf, hnbuf); #elif defined(C_MPI_PRACTICAL) || defined(F_MPI_PRACTICAL) || defined(C_OPENSHMEM_PRACTICAL) || defined(F_OPENSHMEM_PRACTICAL) printf("Rank %d on core %s of node <%s>\n", rank, clbuf, hnbuf); #elif defined(C_OPENMP_PRACTICAL) || defined(F_OPENMP_PRACTICAL) printf("Thread %d on core %s\n", thread, clbuf); #else /* if defined(C_SERIAL_PRACTICAL) || defined(F_SERIAL_PRACTICAL) */ printf("Program on core %s\n",clbuf); #endif /* C_HYBRID_PRACTICAL || F_HYBRID_PRACTICAL */ #if defined(C_OPENSHMEM_PRACTICAL) || defined(F_OPENSHMEM_PRACTICAL) MPI_Finalize(); #endif /* C_OPENSHMEM_PRACTICAL || F_OPENSHMEM_PRACTICAL */ }
int main(int argc, char *argv[]) { cpu_set_t new_mask, cur_mask; pid_t pid = 0; int opt, err; char mstr[1 + CPU_SETSIZE / 4]; char cstr[7 * CPU_SETSIZE]; int c_opt = 0; struct option longopts[] = { { "pid", 0, NULL, 'p' }, { "cpu-list", 0, NULL, 'c' }, { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'V' }, { NULL, 0, NULL, 0 } }; while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) { int ret = 1; switch (opt) { case 'p': pid = atoi(argv[argc - 1]); break; case 'c': c_opt = 1; break; case 'V': printf("taskset version " VERSION "\n"); return 0; case 'h': ret = 0; default: show_usage(argv[0]); return ret; } } if ((!pid && argc - optind < 2) || (pid && (argc - optind < 1 || argc - optind > 2))) { show_usage(argv[0]); return 1; } if (pid) { if (sched_getaffinity(pid, sizeof (cur_mask), &cur_mask) < 0) { perror("sched_getaffinity"); fprintf(stderr, "failed to get pid %d's affinity\n", pid); return 1; } if (c_opt) printf("pid %d's current affinity list: %s\n", pid, cpuset_to_cstr(&cur_mask, cstr)); else printf("pid %d's current affinity mask: %s\n", pid, cpuset_to_str(&cur_mask, mstr)); if (argc - optind == 1) return 0; } if (c_opt) err = cstr_to_cpuset(&new_mask, argv[optind]); else err = str_to_cpuset(&new_mask, argv[optind]); if (err) { if (c_opt) fprintf(stderr, "failed to parse CPU list %s\n", argv[optind]); else fprintf(stderr, "failed to parse CPU mask %s\n", argv[optind]); return 1; } if (sched_setaffinity(pid, sizeof (new_mask), &new_mask)) { perror("sched_setaffinity"); fprintf(stderr, "failed to set pid %d's affinity.\n", pid); return 1; } if (sched_getaffinity(pid, sizeof (cur_mask), &cur_mask) < 0) { perror("sched_getaffinity"); fprintf(stderr, "failed to get pid %d's affinity.\n", pid); return 1; } if (pid) { if (c_opt) printf("pid %d's new affinity list: %s\n", pid, cpuset_to_cstr(&cur_mask, cstr)); else printf("pid %d's new affinity mask: %s\n", pid, cpuset_to_str(&cur_mask, mstr)); } else { argv += optind + 1; execvp(argv[0], argv); perror("execvp"); fprintf(stderr, "failed to execute %s\n", argv[0]); return 1; } return 0; }