我正在尝试测量内存的写入带宽,我创建了一个 8G 字符数组,并使用 128 个线程在其上调用 memset。下面是代码片段。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
int64_t char_num = 8000000000;
int threads = 128;
int res_num = 62500000;
uint8_t* arr;
static inline double timespec_to_sec(struct timespec t)
{
return t.tv_sec * 1.0 + t.tv_nsec / 1000000000.0;
}
void* multithread_memset(void* val) {
int thread_id = *(int*)val;
memset(arr + (res_num * thread_id), 1, res_num);
return NULL;
}
void start_parallel()
{
int* thread_id = malloc(sizeof(int) * threads);
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud)