将功能卸载到英特尔至强融核所需的时间

Required time to offload a function to Intel Xeon Phi

卸载调用将函数的数据(参数)从主机传输到英特尔 MIC(至强融核协处理器 3120 系列)是否需要预定义的时间?

具体来说,我对要在 MIC 上执行的函数执行卸载调用(“#pragma offload target(mic)”)。该函数有 15 个参数(指针和变量),我已经在 MIC 上确认了参数的正确传递。然而,我简化了代码,目的是检查参数传递的时间,因此它只包含一个简单的 "printf()" 函数。我使用 "sys/time.h" 头文件的 "gettimeofday()" 来测量时间,如下面的代码所示:

主机部分硬件信息: Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz / CentOS 版本 6.8 / PCI Express 修订版 2.0

main.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

__attribute__ (( target (mic))) unsigned long long ForSolution = 0;
__attribute__ (( target (mic))) unsigned long long sufficientSol = 1;
__attribute__ (( target (mic))) float timer = 0.0;

__attribute__ (( target (mic))) void function(float *grid, float *displ, unsigned long long *li, unsigned long long *repet, float *solution, unsigned long long dim, unsigned long long numOfa, unsigned long long numLoops, unsigned long long numBlock, unsigned long long thread, unsigned long long blockGrid, unsigned long long station, unsigned long long bytesSol, unsigned long long totalSol, volatile unsigned long long *prog);

   float    *grid, *displ, *solution;
   unsigned long long   *li,repet;
   volatile unsigned long long  *prog;
   unsigned long long dim = 10, grid_a = 3, numLoops = 2, numBlock = 0;
   unsigned long long thread = 220, blockGrid = 0, station = 12;
   unsigned long long station_at = 8, bytesSol, totalSol;

   bytesSol = dim*sizeof(float);
   totalSol = ((1024 * 1024 * 1024) / bytesSol) * bytesSol;



   /******** Some memcpy() functions here for the pointers*********/                   



gettimeofday(&start, NULL);

   #pragma offload target(mic) \
        in(grid:length(dim * grid_a * sizeof(float))) \
        in(displ:length(station * station_at * sizeof(float))) \
        in(li:length(dim * sizeof(unsigned long long))) \
        in(repet:length(dim * sizeof(unsigned long long))) \
        out(solution:length(totalSol/sizeof(float))) \
        in(dim,grid_a,numLoops,numBlock,thread,blockGrid,station,bytesSol,totalSol) \
        in(prog:length(sizeof(volatile unsigned long long))) \
        inout(ForSolution,sufficientSol,timer)
   {
        function(grid, displ, li, repet, solution, dim, grid_a, numLoops, numBlock, thread, blockGrid, station, bytesSol, totalSol, prog);
   }

    gettimeofday(&end, NULL);  

    printf("Time to tranfer data on Intel Xeon Phi: %f sec\n", (((end.tv_sec - start.tv_sec) * 1000000.0 + (end.tv_usec - start.tv_usec)) / 1000000.0) - timer);
    printf("Time for calculations: %f sec\n", timer);

function.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <omp.h>

void function(float *grid, float *displ, unsigned long long *li, unsigned long long *repet, float *solution, unsigned long long dim, unsigned long long numOfa, unsigned long long numLoops, unsigned long long numBlock, unsigned long long thread, unsigned long long blockGrid, unsigned long long station, unsigned long long bytesSol, unsigned long long totalSol, volatile unsigned long long *prog)
{
    struct timeval      timer_start, timer_end;

    gettimeofday(&timer_start, NULL);

printf("Hello World!!!\n");


    gettimeofday(&timer_end, NULL);

    timer = ((timer_end.tv_sec - timer_start.tv_sec) * 1000000.0 + (timer_end.tv_usec - timer_start.tv_usec)) / 1000000.0 ;  
}

终端结果:

Time to tranfer data on Intel Xeon Phi: 3.512706 sec
Time for calculations: 0.000002 sec
Hello World!!!

代码需要 3.5 秒才能完成 "offload target"。以上结果正常吗?有什么办法可以减少卸载调用的显着时间延迟吗?

我们来看看这里的步骤:

a) 对于第一个 #pragma offload,MIC 被初始化;这可能包括重置它、​​启动精简的 Linux(并等待它启动所有 CPU、初始化其内存管理、启动伪 NIC 驱动程序等),以及将您的代码上传到设备。这可能需要几秒钟的时间。

b) 所有输入数据都上传到MIC。

c) 函数执行完毕

d) 所有的输出数据都是从MIC下载的。

对于通过 PCI Express 修订版 2.0 (x16) 的原始数据传输,最大值。带宽为 8 GB/s;但是你不会得到最大值。带宽。根据我的记忆,与 Phi 的通信涉及共享环形缓冲区和 "doorbell" IRQ,双方(在主机和协处理器的 OS 上)都有 "pseudo NIC" 驱动程序;加上所有的握手和开销,如果你能得到最大的一半,我会感到惊讶。带宽。

我认为上传的代码、上传的数据和下载的数据总量远远超过 1 GiB(例如 out(solution:length(totalSol/sizeof(float))) 本身就是 1 GiB)。如果我们假设 "about 4 GiB/s" 至少还有 ~250 毫秒。

我的建议是每件事都做两次;并测量第一次(包括初始化所有内容)和第二次(当所有内容都已初始化时)之间的差异,以确定初始化协处理器需要多长时间。第二次测量(减去执行函数的时间)将告诉您数据传输花费了多长时间。