Skip to content

Commit

Permalink
Use C string for affinity domain tag
Browse files Browse the repository at this point in the history
  • Loading branch information
TomTheBear committed Nov 15, 2024
1 parent ab18b9f commit 100d843
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 43 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ install: install_daemon install_freq install_appdaemon install_container_helper
@install -m 644 $(SRC_DIR)/includes/likwid.h $(PREFIX)/include/
@sed -i -e "s#<VERSION>#$(VERSION)#g" -e "s#<DATE>#$(DATE)#g" -e "s#<GITCOMMIT>#$(GITCOMMIT)#g" -e "s#<MINOR>#$(MINOR)#g" $(PREFIX)/include/likwid.h
@install -m 644 $(SRC_DIR)/includes/likwid-marker.h $(PREFIX)/include/
@install -m 644 $(SRC_DIR)/includes/bstrlib.h $(PREFIX)/include/
$(FORTRAN_INSTALL)
@echo "===> INSTALL groups to $(PREFIX)/share/likwid/perfgroups"
@mkdir -p $(PREFIX)/share/likwid/perfgroups
Expand All @@ -622,7 +621,6 @@ install: install_daemon install_freq install_appdaemon install_container_helper
@echo "===> INSTALL docs and examples to $(PREFIX)/share/likwid/docs"
@mkdir -p $(PREFIX)/share/likwid/docs
@chmod 755 $(PREFIX)/share/likwid/docs
@install -m 644 $(DOC_DIR)/bstrlib.txt $(PREFIX)/share/likwid/docs
@mkdir -p $(PREFIX)/share/likwid/examples
@chmod 755 $(PREFIX)/share/likwid/examples
@install -m 644 $(EXAMPLES_DIR)/* $(PREFIX)/share/likwid/examples
Expand Down Expand Up @@ -679,7 +677,6 @@ move: move_daemon move_freq move_appdaemon move_container_helper
@chmod 755 $(INSTALLED_PREFIX)/include
@install -m 644 $(PREFIX)/include/likwid.h $(INSTALLED_PREFIX)/include/likwid.h
@install -m 644 $(PREFIX)/include/likwid-marker.h $(INSTALLED_PREFIX)/include/likwid-marker.h
@install -m 644 $(PREFIX)/include/bstrlib.h $(INSTALLED_PREFIX)/include/bstrlib.h
@if [ -e $(PREFIX)/include/likwid.mod ]; then install $(PREFIX)/include/likwid.mod $(INSTALLED_PREFIX)/include/likwid.mod; fi
@echo "===> MOVE groups from $(PREFIX)/share/likwid/perfgroups to $(INSTALLED_PREFIX)/share/likwid/perfgroups"
@mkdir -p $(INSTALLED_PREFIX)/share/likwid/perfgroups
Expand All @@ -690,7 +687,6 @@ move: move_daemon move_freq move_appdaemon move_container_helper
@find $(INSTALLED_PREFIX)/share/likwid/perfgroups -name "*.txt" -exec chmod 644 {} \;
@mkdir -p $(INSTALLED_PREFIX)/share/likwid/docs
@chmod 755 $(INSTALLED_PREFIX)/share/likwid/docs
@install -m 644 $(PREFIX)/share/likwid/docs/bstrlib.txt $(INSTALLED_PREFIX)/share/likwid/docs
@mkdir -p $(INSTALLED_PREFIX)/share/likwid/examples
@chmod 755 $(INSTALLED_PREFIX)/share/likwid/examples
@install -m 644 $(EXAMPLES_DIR)/* $(INSTALLED_PREFIX)/share/likwid/examples
Expand Down Expand Up @@ -733,7 +729,6 @@ uninstall: uninstall_daemon uninstall_freq uninstall_appdaemon uninstall_contain
@echo "===> REMOVING header from $(PREFIX)/include"
@rm -f $(PREFIX)/include/likwid.h
@rm -f $(PREFIX)/include/likwid-marker.h
@rm -f $(PREFIX)/include/bstrlib.h
$(FORTRAN_REMOVE)
@echo "===> REMOVING filter, groups and default configs from $(PREFIX)/share/likwid"
@rm -rf $(abspath $(PREFIX)/share/likwid/filter)
Expand Down Expand Up @@ -770,7 +765,6 @@ uninstall_moved: uninstall_daemon_moved uninstall_freq_moved uninstall_appdaemon
@echo "===> REMOVING header from $(INSTALLED_PREFIX)/include"
@rm -f $(INSTALLED_PREFIX)/include/likwid.h
@rm -f $(PREFIX)/include/likwid-marker.h
@rm -f $(INSTALLED_PREFIX)/include/bstrlib.h
$(FORTRAN_REMOVE)
@echo "===> REMOVING filter, groups and default configs from $(INSTALLED_PREFIX)/share/likwid"
@rm -rf $(LIKWIDFILTERPATH)
Expand Down
2 changes: 1 addition & 1 deletion bench/likwid-bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ int main(int argc, char** argv)
for (i=0; i < affinity->numberOfAffinityDomains; i++ )
{
ownprintf("Domain %d:\n",i);
ownprintf("\tTag %s:",bdata(affinity->domains[i].tag));
ownprintf("\tTag %s:",affinity->domains[i].tag);

for ( uint32_t j=0; j < affinity->domains[i].numberOfProcessors; j++ )
{
Expand Down
4 changes: 2 additions & 2 deletions bench/src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ allocator_allocateVector(

for (i=0;i<domains->numberOfAffinityDomains;i++)
{
if (biseq(domainString, domains->domains[i].tag))
if (strncmp(domains->domains[i].tag, bdata(domainString), strlen(domains->domains[i].tag)) == 0)
{
domain = domains->domains + i;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ allocator_allocateVector(
affinity_pinProcess(domain->processorList[0]);
printf("Allocate: Process running on hwthread %d (Domain %s) - Vector length %llu/%llu Offset %d Alignment %llu\n",
affinity_processGetProcessorId(),
bdata(domain->tag),
domain->tag,
LLU_CAST size,
LLU_CAST bytesize,
offset,
Expand Down
64 changes: 47 additions & 17 deletions src/affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ static int affinity_addNodeDomain(AffinityDomain* domain, int* help)
{
return -ENOMEM;
}
domain->tag = malloc(2);
if (!domain->tag)
{
free(domain->processorList);
return -ENOMEM;
}
int offset = 0;
int cores = 0;
for (int i = 0; i < MAX(cputopo->numSockets, 1); i++)
Expand All @@ -537,7 +543,8 @@ static int affinity_addNodeDomain(AffinityDomain* domain, int* help)
}
domain->numberOfProcessors = offset;
domain->numberOfCores = cores;
domain->tag = bformat("N");
domain->tag[0] = 'N';
domain->tag[1] = '\0';
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain N: %d HW threads on %d cores, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
Expand All @@ -559,6 +566,12 @@ static int affinity_addSocketDomain(int socket, AffinityDomain* domain, int* hel
{
return -ENOMEM;
}
domain->tag = malloc(10);
if (!domain->tag)
{
free(domain->processorList);
return -ENOMEM;
}
int tmp = treeFillNextEntries(cputopo->topologyTree,
domain->processorList,
0,
Expand All @@ -568,7 +581,7 @@ static int affinity_addSocketDomain(int socket, AffinityDomain* domain, int* hel
tmp = MIN(tmp, domain->numberOfProcessors);
domain->numberOfProcessors = tmp;
domain->numberOfCores = affinity_countSocketCores(domain->numberOfProcessors, domain->processorList, help);
domain->tag = bformat("S%d", socket);
snprintf(domain->tag, 9, "S%d", socket);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain S%d: %d HW threads on %d cores, socket, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
Expand All @@ -594,6 +607,12 @@ static int affinity_addDieDomain(int socket, int die, AffinityDomain* domain, in
{
return -ENOMEM;
}
domain->tag = malloc(10);
if (!domain->tag)
{
free(domain->processorList);
return -ENOMEM;
}
domain->numberOfProcessors = numThreadsPerDie;
domain->numberOfCores = numCoresPerDie;
int tmp = treeFillNextEntries(cputopo->topologyTree,
Expand All @@ -605,7 +624,7 @@ static int affinity_addDieDomain(int socket, int die, AffinityDomain* domain, in
domain->numberOfProcessors);
domain->numberOfProcessors = tmp;
domain->numberOfCores = affinity_countSocketCores(domain->numberOfProcessors, domain->processorList, help);
domain->tag = bformat("D%d", dieId);
snprintf(domain->tag, 9, "D%d", dieId);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain D%d: %d HW threads on %d cores, dieId, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
Expand All @@ -630,6 +649,12 @@ static int affinity_addCacheDomain(int socket, int cacheId, AffinityDomain* doma
{
return -ENOMEM;
}
domain->tag = malloc(10);
if (!domain->tag)
{
free(domain->processorList);
return -ENOMEM;
}
domain->numberOfProcessors = numThreadsPerCache;
domain->numberOfCores = numCoresPerCache;
int tmp = treeFillNextEntries(cputopo->topologyTree,
Expand All @@ -641,7 +666,7 @@ static int affinity_addCacheDomain(int socket, int cacheId, AffinityDomain* doma
domain->numberOfProcessors);
domain->numberOfProcessors = tmp;
domain->numberOfCores = affinity_countSocketCores(domain->numberOfProcessors, domain->processorList, help);
domain->tag = bformat("C%d", cid);
snprintf(domain->tag, 9, "C%d", cid);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain C%d: %d HW threads on %d cores, cid, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
Expand All @@ -666,6 +691,12 @@ static int _affinity_addMemoryDomain(int nodeId, AffinityDomain* domain, int* he
{
return -ENOMEM;
}
domain->tag = malloc(10);
if (!domain->tag)
{
free(domain->processorList);
return -ENOMEM;
}
for (int i = 0; i < numatopo->nodes[nodeId].numberOfProcessors; i++)
{
for (int j = 0; j < cputopo->numHWThreads; j++)
Expand All @@ -678,8 +709,7 @@ static int _affinity_addMemoryDomain(int nodeId, AffinityDomain* domain, int* he
}
domain->numberOfProcessors = num_hwthreads;
domain->numberOfCores = affinity_countSocketCores(domain->numberOfProcessors, domain->processorList, help);
domain->tag = bformat("M%d", nodeId);

snprintf(domain->tag, 9, "M%d", nodeId);
return 0;
}
}
Expand Down Expand Up @@ -718,9 +748,8 @@ static int affinity_addCudaDomain(int nodeId, AffinityDomain* domain, int offset
err = _affinity_addMemoryDomain(cudadev->numaNode, domain, help);
if (err == 0)
{
bdestroy(domain->tag);
domain->tag = bformat("G%d", nodeId+offset);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain %s: %d HW threads on %d cores, bdata(domain->tag), domain->numberOfProcessors, domain->numberOfCores);
snprintf(domain->tag, 9, "G%d", nodeId+offset);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain %s: %d HW threads on %d cores, domain->tag, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
}
Expand Down Expand Up @@ -752,9 +781,8 @@ static int affinity_addRocmDomain(int nodeId, AffinityDomain* domain, int offset
err = _affinity_addMemoryDomain(rocmdev->numaNode, domain, help);
if (err == 0)
{
bdestroy(domain->tag);
domain->tag = bformat("G%d", nodeId+offset);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain %s: %d HW threads on %d cores, bdata(domain->tag), domain->numberOfProcessors, domain->numberOfCores);
snprintf(domain->tag, 9, "G%d", nodeId+offset);
DEBUG_PRINT(DEBUGLEV_DEVELOP, Affinity domain %s: %d HW threads on %d cores, domain->tag, domain->numberOfProcessors, domain->numberOfCores);
return 0;
}
}
Expand Down Expand Up @@ -1022,8 +1050,10 @@ affinity_finalize()
}
for ( int i=0; i < affinityDomains.numberOfAffinityDomains; i++ )
{
if (affinityDomains.domains[i].tag)
bdestroy(affinityDomains.domains[i].tag);
if (affinityDomains.domains[i].tag != NULL)
{
free(affinityDomains.domains[i].tag);
}
if (affinityDomains.domains[i].processorList != NULL)
{
free(affinityDomains.domains[i].processorList);
Expand Down Expand Up @@ -1179,12 +1209,12 @@ affinity_pinProcesses(int cpu_count, const int* processorIds)
}

const AffinityDomain*
affinity_getDomain(bstring domain)
affinity_getDomain(char* domain)
{

for ( int i=0; i < affinity_numberOfDomains; i++ )
{
if ( biseq(domain, domains[i].tag) )
if ( strncmp(domains[i].tag, domain, strlen(domains[i].tag)) == 0 )
{
return domains+i;
}
Expand All @@ -1199,7 +1229,7 @@ affinity_printDomains()
for ( int i=0; i < affinity_numberOfDomains; i++ )
{
printf("Domain %d:\n",i);
printf("\tTag %s:",bdata(domains[i].tag));
printf("\tTag %s:", domains[i].tag);

for ( uint32_t j=0; j < domains[i].numberOfProcessors; j++ )
{
Expand Down
28 changes: 14 additions & 14 deletions src/cpustring.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ cpu_in_domain(int domainidx, int cpu)
}

static int
get_domain_idx(bstring bdomain)
get_domain_idx(char* bdomain)
{
affinity_init();
AffinityDomains_t affinity = get_affinityDomains();
for (int i=0;i<affinity->numberOfAffinityDomains; i++)
{
if (bstrcmp(affinity->domains[i].tag, bdomain) == BSTR_OK)
if (strncmp(affinity->domains[i].tag, bdomain, strlen(affinity->domains[i].tag)) == BSTR_OK)
{
return i;
}
Expand All @@ -155,7 +155,7 @@ cpuexpr_to_list(bstring bcpustr, bstring prefix, int* list, int length)
oldinsert = insert;
for (int j = 0; j < affinity->numberOfAffinityDomains; j++)
{
if (bstrcmp(affinity->domains[j].tag, newstr) == 0)
if (strncmp(affinity->domains[j].tag, bdata(newstr), strlen(affinity->domains[j].tag)) == 0)
{
tmp = check_and_atoi(bdata(strlist->entry[i]));
if (tmp >= 0)
Expand Down Expand Up @@ -213,7 +213,7 @@ cpustr_to_cpulist_method(bstring bcpustr, int* cpulist, int length)
}
for (int i=0; i<affinity->numberOfAffinityDomains; i++)
{
if (binstr(affinity->domains[i].tag, 0, parts->entry[0]) != BSTR_ERR &&
if (strstr(affinity->domains[i].tag, bdata(parts->entry[0])) != NULL &&
affinity->domains[i].numberOfProcessors > 0)
{
suitable[suitidx] = i;
Expand Down Expand Up @@ -387,12 +387,12 @@ cpustr_to_cpulist_expression(bstring bcpustr, int* cpulist, int length)
CpuTopology_t cpuid_topology = get_cpuTopology();
affinity_init();
AffinityDomains_t affinity = get_affinityDomains();
bstring bdomain;
int domainidx = -1;
int count = 0;
int stride = 0;
int chunk = 0;
int off = 0;
bstring bdomain;
if (bstrchrp(bcpustr, 'E', 0) != 0)
{
fprintf(stderr, "Not a valid CPU expression\n");
Expand Down Expand Up @@ -436,7 +436,7 @@ cpustr_to_cpulist_expression(bstring bcpustr, int* cpulist, int length)
bstrListDestroy(strlist);
return 0;
}
domainidx = get_domain_idx(bdomain);
domainidx = get_domain_idx(bdata(bdomain));
if (domainidx < 0)
{
fprintf(stderr, "Cannot find domain %s\n", bdata(bdomain));
Expand Down Expand Up @@ -505,7 +505,7 @@ cpustr_to_cpulist_logical(bstring bcpustr, int* cpulist, int length)
bdomain = bstrcpy(strlist->entry[1]);
blist = bstrcpy(strlist->entry[2]);
bstrListDestroy(strlist);
domainidx = get_domain_idx(bdomain);
domainidx = get_domain_idx(bdata(bdomain));
if (domainidx < 0)
{
fprintf(stderr, "ERROR: Cannot find domain %s\n", bdata(bdomain));
Expand Down Expand Up @@ -565,7 +565,7 @@ cpustr_to_cpulist_logical(bstring bcpustr, int* cpulist, int length)
{
fprintf(stderr,
"WARN: Selected affinity domain %s has only %d hardware threads, but selection string evaluates to %d threads.\n",
bdata(affinity->domains[domainidx].tag), ret, require);
affinity->domains[domainidx].tag, ret, require);
fprintf(stderr, " This results in multiple threads on the same hardware thread.\n");
return 0;
}
Expand Down Expand Up @@ -680,15 +680,15 @@ cpustr_to_cpulist_physical(bstring bcpustr, int* cpulist, int length)
bdomain = bformat("N");
blist = bstrcpy(bcpustr);
}
domainidx = get_domain_idx(bdomain);
domainidx = get_domain_idx(bdata(bdomain));
if (domainidx < 0)
{
fprintf(stderr, "Cannot find domain %s\n", bdata(bdomain));
bdestroy(bdomain);
bdestroy(blist);
return 0;
}
bstring domtag = affinity->domains[domainidx].tag;
char* domtag = affinity->domains[domainidx].tag;

strlist = bsplit(blist, ',');
int insert = 0;
Expand Down Expand Up @@ -731,7 +731,7 @@ cpustr_to_cpulist_physical(bstring bcpustr, int* cpulist, int length)
notInCpuSet = 1;
}
}
fprintf(stderr, "CPU %d not in domain %s.", j, bdata(domtag));
fprintf(stderr, "CPU %d not in domain %s.", j, domtag);
if (notInCpuSet)
{
fprintf(stderr, " It is not in the given cpuset\n");
Expand Down Expand Up @@ -767,7 +767,7 @@ cpustr_to_cpulist_physical(bstring bcpustr, int* cpulist, int length)
notInCpuSet = 1;
}
}
fprintf(stderr, "CPU %d not in domain %s.", j, bdata(domtag));
fprintf(stderr, "CPU %d not in domain %s.", j, domtag);
if (notInCpuSet)
{
fprintf(stderr, " It is not in the given cpuset\n");
Expand Down Expand Up @@ -808,7 +808,7 @@ cpustr_to_cpulist_physical(bstring bcpustr, int* cpulist, int length)
notInCpuSet = 1;
}
}
fprintf(stderr, "CPU %d not in domain %s.", cpu, bdata(domtag));
fprintf(stderr, "CPU %d not in domain %s.", cpu, domtag);
if (notInCpuSet)
{
fprintf(stderr, " It is not in the given cpuset\n");
Expand Down Expand Up @@ -889,7 +889,7 @@ cpustr_to_cpulist(const char* cpustring, int* cpulist, int length)
}
else
{
int dom = get_domain_idx(strlist->entry[i]);
int dom = get_domain_idx(bdata(strlist->entry[i]));
if (dom >= 0)
{
AffinityDomains_t affinity = get_affinityDomains();
Expand Down
2 changes: 1 addition & 1 deletion src/includes/affinity.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ extern int *affinity_thread2die_lookup;
extern int *affinity_thread2sharedl3_lookup;
extern int affinity_processGetProcessorId();
extern int affinity_threadGetProcessorId();
extern const AffinityDomain* affinity_getDomain(bstring domain);
extern const AffinityDomain* affinity_getDomain(char* domain);

#endif /*AFFINITY_H*/
2 changes: 1 addition & 1 deletion src/includes/likwid.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ system. Example domains are NUMA nodes, CPU sockets/packages or LLC (Last Level
Cache) cache domains. \extends AffinityDomains
*/
typedef struct {
bstring tag; /*!< \brief Bstring with the ID for the affinity domain.
char* tag; /*!< \brief Bstring with the ID for the affinity domain.
Currently possible values: N (node), SX (socket/package X), CX
(LLC cache domain X) and MX (memory domain X) */
uint32_t numberOfProcessors; /*!< \brief Number of HW threads in the domain
Expand Down
Loading

0 comments on commit 100d843

Please sign in to comment.