現在、システム メトリックに PAPI を使用できますが、アプリケーションでのみ使用できます。構造は-- ライブラリの初期化 -- イベントの作成 -- 開始イベント (命令の総数および/またはサイクルの総数) -- ワークロード (アプリケーションのコード) -- 停止イベント (測定値を返す)です。しかし、私は別の方法でアプローチしたい- ライブラリを初期化する - イベントを作成する - イベントを開始する(命令の総数および/またはサイクルの総数) - " SLEEP " - イベントを停止する(測定値を返す)。したがって、基本的には、ある端末でプログラムを実行し、別の端末 (スリープ) で PAPI 実装を実行して、測定値を取得できるようにしたいと考えています。
以下に最初の構造を示します。
#include <iostream>
#include <papi.h>
void powersOfOne(int);
int main()
{
int retval;
retval = PAPI_library_init(PAPI_VER_CURRENT);
if (retval != PAPI_VER_CURRENT)
{
std::cerr << "PAPI: init. failed: " << PAPI_strerror(retval) << std::endl;
return 1;
}
/* set up event set */
int eventset = PAPI_NULL;
retval = PAPI_create_eventset(&eventset);
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not create eventset: " << PAPI_strerror(retval) << std::endl;
return 1;
}
//adding total_cycles and total_instructions to event
retval = PAPI_add_named_event(eventset, "PAPI_TOT_CYC");
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not add PAPI_TOT_CYC: " << PAPI_strerror(retval) << std::endl;
}
retval = PAPI_add_named_event(eventset, "PAPI_TOT_INS");
if (retval != PAPI_OK)
{
std::cerr << "PAPI: could not add PAPI_TOT_INS: " << PAPI_strerror(retval) << std::endl;
}
/* sample */
long long count;
PAPI_reset(eventset);
retval = PAPI_start(eventset);
if (retval != PAPI_OK)
std::cerr << "PAPI: could not START sample: " << PAPI_strerror(retval) << std::endl;
/* the workload */
powersOfOne(10);
//stopping eventset
retval = PAPI_stop(eventset, count);
if (retval != PAPI_OK)
std::cerr << "PAPI: could not STOP sample: " << PAPI_strerror(retval) << std::endl;
std::cout << "PAPI: measured " << count << " cycles" << std::endl;
std::cout << "PAPI: measured " << count << " instructions" << std::endl;
/* (optional) cleanup */
PAPI_cleanup_eventset(eventset);
PAPI_destroy_eventset(&eventset);
return 0;
}
void powersOfOne(int num){
int val = 1;
for (int i = 1; i < num; i++){
val*=1;
}
std::cout << val << std::endl;
}