开发者

How to write a program testing the bandwidth between CPU and Memory?(Using C or C++) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

follow my question开发者_StackOverflow title. And I realize it using the code below:

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

#define COFF (1 << 20) //1MB
#define LOW_BYTE (1 << 10) //1kb
#define UP_BYTE (1 << 23) //8MB     
#define MAX UP_BYTE/sizeof(double)

double a[MAX] = {1};

double current_time(void)
{
    double timestamp;

    struct timeval tv;
    gettimeofday(&tv, 0);

    timestamp = (double)((double)(tv.tv_sec*1e6) +(double)tv.tv_usec);
    return timestamp;
}

void test_band_width(int size){
    int i;
    volatile double r = 0;
    for (i=0; i < size; i+=16) {
        r += a[i]; 
    }
}

int main(int argc, char* argv[]){
    int k, size;
    double cycles;
    double t_start=0.0, t_end=0.0, time=0.0;
    for (k=UP_BYTE; k >= LOW_BYTE; k >>= 1) {
        size = k / sizeof(double);
        t_start = current_time();
        test_band_width(size);
        t_end = current_time();
        //printf("time is %lf\n",t_end-t_start);
        time = (t_end-t_start);
        printf("time2 is %lf\n\n\n",time);
        printf("This time the BandWith is %.7f\n",k/(16*time));
    }
    return 0;
}

But there may be some problem when I tested my PC, maybe the way I test the systime?


There are several ways to do bandwidth tests and they all measures different things. Search for the stream benchmarks for instance or look at http://www.akkadia.org/drepper/cpumemory.pdf which gives pointer to some other tool.

One basic test is to have an inner loop like:

void** ptr = ...;

while (ptr != NULL) {
    ptr = (void**)*ptr;
}

and initialize your memory beforehand with the access pattern you want to test. (Measure for instance with different stride, going forward/backward,...)


Depending on your target platform, using floating-point types such as double can significantly interfere with the performance you're trying to measure. I'd change your code to use pure integer operations throughout.


There are expert timings for these things, with code, here: http://www.agner.org/optimize/#testp

That site is well worth a read - you may get your numbers there, rather than writing or running your own tests!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜