<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to compile Code having PCM API? in Software Tuning, Performance Optimization &amp; Platform Monitoring</title>
    <link>https://community.intel.com/t5/Software-Tuning-Performance/How-to-compile-Code-having-PCM-API/m-p/1317418#M7946</link>
    <description>&lt;P&gt;The example needs to be linked against libpcm. This should be everything you need.&lt;BR /&gt;You might also want to have a look at the &lt;A href="https://github.com/opcm/pcm/blob/582cb6e4ebf604146b205d57b1951e303c2cd006/Makefile#L93" target="_self"&gt;Makefile&lt;/A&gt; of the project. It includes the rules to compile and &lt;A href="https://github.com/opcm/pcm/blob/582cb6e4ebf604146b205d57b1951e303c2cd006/Makefile#L93" target="_self"&gt;link&lt;/A&gt; the example.&lt;/P&gt;</description>
    <pubDate>Mon, 27 Sep 2021 10:21:56 GMT</pubDate>
    <dc:creator>Thomas_W_Intel</dc:creator>
    <dc:date>2021-09-27T10:21:56Z</dc:date>
    <item>
      <title>How to compile Code having PCM API?</title>
      <link>https://community.intel.com/t5/Software-Tuning-Performance/How-to-compile-Code-having-PCM-API/m-p/1316317#M7941</link>
      <description>&lt;P&gt;There is a piece of code (c_example.c) that comes with the PCM installation. Can someone tell me how to compile that i.e which libraries it needs to be linked against. Below is the code.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;#include "cpucounters.h"
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;dlfcn.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int pcm_getcpu()
{
        int id = -1;
        asm volatile (
                "rdtscp\n\t"
                "mov %%ecx, %0\n\t":
                "=r" (id) :: "%rax", "%rcx", "%rdx");
        // processor ID is in ECX: https://www.felixcloutier.com/x86/rdtscp
        // Linux encodes the NUMA node starting at bit 12, so remove the NUMA
        // bits when returning the CPU integer by masking with 0xFFF.
        return id &amp;amp; 0xFFF;
}

struct {
        int (*pcm_c_build_core_event)(uint8_t id, const char * argv);
        int (*pcm_c_init)();
        void (*pcm_c_start)();
        void (*pcm_c_stop)();
        uint64_t (*pcm_c_get_cycles)(uint32_t core_id);
        uint64_t (*pcm_c_get_instr)(uint32_t core_id);
        uint64_t (*pcm_c_get_core_event)(uint32_t core_id, uint32_t event_id);
} PCM;

#ifndef PCM_DYNAMIC_LIB
/* Library functions declaration (instead of .h file) */
int pcm_c_build_core_event(uint8_t, const char *);
int pcm_c_init();
void pcm_c_start();
void pcm_c_stop();
uint64_t pcm_c_get_cycles(uint32_t);
uint64_t pcm_c_get_instr(uint32_t);
uint64_t pcm_c_get_core_event(uint32_t, uint32_t);
#endif


int main(int argc, const char *argv[])
{
        int i,a[100],b[100],c[100];
        uint32_t total = 0;
        int lcore_id;

        /* Seed for predictable rand() results */
        srand(0);
        for (i=0; i &amp;lt; 100; ++i) {
                a[i] = rand();
                b[i] = rand();
                c[i] = rand();
        }

#ifdef PCM_DYNAMIC_LIB
        void * handle = dlopen("libpcm.so", RTLD_LAZY);
        if(!handle) {
                printf("Abort: could not (dynamically) load shared library \n");
                return -1;
        }

        PCM.pcm_c_build_core_event = (int (*)(uint8_t, const char *)) dlsym(handle, "pcm_c_build_core_event");
        PCM.pcm_c_init = (int (*)()) dlsym(handle, "pcm_c_init");
        PCM.pcm_c_start = (void (*)()) dlsym(handle, "pcm_c_start");
        PCM.pcm_c_stop = (void (*)()) dlsym(handle, "pcm_c_stop");
        PCM.pcm_c_get_cycles = (uint64_t (*)(uint32_t)) dlsym(handle, "pcm_c_get_cycles");
        PCM.pcm_c_get_instr = (uint64_t (*)(uint32_t)) dlsym(handle, "pcm_c_get_instr");
        PCM.pcm_c_get_core_event = (uint64_t (*)(uint32_t,uint32_t)) dlsym(handle, "pcm_c_get_core_event");
#else
        PCM.pcm_c_build_core_event = pcm_c_build_core_event;
        PCM.pcm_c_init = pcm_c_init;
        PCM.pcm_c_start = pcm_c_start;
        PCM.pcm_c_stop = pcm_c_stop;
        PCM.pcm_c_get_cycles = pcm_c_get_cycles;
        PCM.pcm_c_get_instr = pcm_c_get_instr;
        PCM.pcm_c_get_core_event = pcm_c_get_core_event;
#endif

        if(PCM.pcm_c_init == NULL || PCM.pcm_c_start == NULL || PCM.pcm_c_stop == NULL ||
                        PCM.pcm_c_get_cycles == NULL || PCM.pcm_c_get_instr == NULL ||
                        PCM.pcm_c_build_core_event == NULL)
                return -1;
        switch(argc-1)
        {
                case 4:
                        PCM.pcm_c_build_core_event(3,argv[3]);
                case 3:
                        PCM.pcm_c_build_core_event(2,argv[2]);
                case 2:
                        PCM.pcm_c_build_core_event(1,argv[2]);
                case 1:
                        PCM.pcm_c_build_core_event(0,argv[1]);
                case 0:
                        break;
                default:
                        printf("Number of arguments are too many! exit...\n");
                        return -2;
        }

        printf("[c_example] Initializing PCM measurements:\n");
        PCM.pcm_c_init();

        printf("[c_example] Calling PCM start()\n");
        PCM.pcm_c_start();
        for(i=0;i&amp;lt;10000;i++)
                c[i%100] = 4 * a[i%100] + b[i%100];
        for(i=0;i&amp;lt;100;i++)
                total += c[i];
        PCM.pcm_c_stop();

        printf("[c_example] PCM measurment stopped, compute result %u\n", total);

        lcore_id = pcm_getcpu();
        printf("C:%lu I:%lu, IPC:%3.2f\n",
                PCM.pcm_c_get_cycles(lcore_id),
                PCM.pcm_c_get_instr(lcore_id),
                (double)PCM.pcm_c_get_instr(lcore_id)/PCM.pcm_c_get_cycles(lcore_id));
        printf("CPU%d E0: %lu, E1: %lu, E2: %lu, E3: %lu\n",
                lcore_id,
                PCM.pcm_c_get_core_event(lcore_id,0),
                PCM.pcm_c_get_core_event(lcore_id,1),
                PCM.pcm_c_get_core_event(lcore_id,2),
                PCM.pcm_c_get_core_event(lcore_id,3));

        return 0;
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 21:55:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Tuning-Performance/How-to-compile-Code-having-PCM-API/m-p/1316317#M7941</guid>
      <dc:creator>totochan1985</dc:creator>
      <dc:date>2021-09-21T21:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to compile Code having PCM API?</title>
      <link>https://community.intel.com/t5/Software-Tuning-Performance/How-to-compile-Code-having-PCM-API/m-p/1317418#M7946</link>
      <description>&lt;P&gt;The example needs to be linked against libpcm. This should be everything you need.&lt;BR /&gt;You might also want to have a look at the &lt;A href="https://github.com/opcm/pcm/blob/582cb6e4ebf604146b205d57b1951e303c2cd006/Makefile#L93" target="_self"&gt;Makefile&lt;/A&gt; of the project. It includes the rules to compile and &lt;A href="https://github.com/opcm/pcm/blob/582cb6e4ebf604146b205d57b1951e303c2cd006/Makefile#L93" target="_self"&gt;link&lt;/A&gt; the example.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Sep 2021 10:21:56 GMT</pubDate>
      <guid>https://community.intel.com/t5/Software-Tuning-Performance/How-to-compile-Code-having-PCM-API/m-p/1317418#M7946</guid>
      <dc:creator>Thomas_W_Intel</dc:creator>
      <dc:date>2021-09-27T10:21:56Z</dc:date>
    </item>
  </channel>
</rss>

