Skip to content

Commit

Permalink
Fix for MarkerAPI if master process is already pinned, e.g. by a prev…
Browse files Browse the repository at this point in the history
…ious OpenMP region with LIKWID's pinning. Fixes #602
  • Loading branch information
TomTheBear committed Sep 12, 2024
1 parent 35f988b commit 4d23735
Showing 1 changed file with 44 additions and 12 deletions.
56 changes: 44 additions & 12 deletions src/libperfctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ likwid_markerInit(void)
bstring bEventStr;
struct bstrList* threadTokens;
struct bstrList* eventStrings;
cpu_set_t cpuset;
char* modeStr = getenv("LIKWID_MODE");
char* eventStr = getenv("LIKWID_EVENTS");
char* cThreadStr = getenv("LIKWID_THREADS");
Expand Down Expand Up @@ -193,29 +194,60 @@ likwid_markerInit(void)

if (debugStr != NULL)
{
perfmon_verbosity = atoi(debugStr);
verbosity = perfmon_verbosity;
verbosity = atoi(debugStr);
perfmon_setVerbosity(verbosity);
}

topology_init();
numa_init();
affinity_init();
hashTable_init();

#ifndef LIKWID_USE_PERFEVENT
HPMmode(atoi(modeStr));
#endif

bThreadStr = bfromcstr(cThreadStr);
threadTokens = bsplit(bThreadStr,',');
num_cpus = threadTokens->qty;
for (i=0; i<num_cpus; i++)
for (i = 0; i < num_cpus; i++)
{
threads2Cpu[i] = ownatoi(bdata(threadTokens->entry[i]));
}
bdestroy(bThreadStr);
bstrListDestroy(threadTokens);

CPU_ZERO(&cpuset);
if (pinStr != NULL)
{
// If LIKWID should pin the threads, we check whether we are already
// in a restricted cpuset with a single hwthread. This happens
// when an OpenMP region is executed before LIKWID_MARKER_INIT.
// When the first thread starts up, LIKWID pins the master process
// and the thread. But we want to get the topology without the restriction
// here. We don't need to set it back because the first operation
// after getting the topology is the pinning of the master process.
int err = sched_getaffinity(0, sizeof(cpu_set_t), &cpuset);
if (err == 0)
{
int count = CPU_COUNT(&cpuset);
if (count == 1 && count != num_cpus)
{
cpu_set_t newcpuset;
CPU_ZERO(&newcpuset);
for (i = 0; i < num_cpus; i++)
{
CPU_SET(threads2Cpu[i], &newcpuset);
}
err = sched_setaffinity(0, sizeof(cpu_set_t), &newcpuset);
}
}
else
{
fprintf(stderr,"Failed to get cpuset for master thread.\n");
}
}

topology_init();
numa_init();
affinity_init();
hashTable_init();

#ifndef LIKWID_USE_PERFEVENT
HPMmode(atoi(modeStr));
#endif

if (pinStr != NULL)
{
likwid_pinThread(threads2Cpu[0]);
Expand Down

0 comments on commit 4d23735

Please sign in to comment.