Skip to content

Instantly share code, notes, and snippets.

@NanXiao
Created July 2, 2018 09:30
Show Gist options
  • Save NanXiao/08c0bf67b0be53897357519663d835fd to your computer and use it in GitHub Desktop.
Save NanXiao/08c0bf67b0be53897357519663d835fd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#define CPU_NUM (104)
#define LOOP_NUM (100)
#define ARRAY_SIZE (CPU_NUM * LOOP_NUM)
double a[ARRAY_SIZE], b[ARRAY_SIZE], c[ARRAY_SIZE];
double mysecond(void)
{
struct timeval tp;
gettimeofday(&tp, NULL);
return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 );
}
void func(double a[ARRAY_SIZE], double b[ARRAY_SIZE], double c[ARRAY_SIZE], int index)
{
for (int i = 0; i < 100000; i++)
{
c[index] = a[index];
a[index] = c[index] * b[index];
b[index] = c[index] / a[index];
}
}
int main(void)
{
for (size_t i = 0; i < ARRAY_SIZE; i++)
{
a[i] = 1.0;
b[i] = 2.0;
}
for (int loop = 0; loop < 10; loop++)
{
double start = mysecond();
#if 1
for (int i = 0; i < LOOP_NUM; i++)
{
#pragma omp parallel
for (int j = 0; j < CPU_NUM; j++)
{
func(a, b, c, i * CPU_NUM + j);
}
}
#else
#pragma omp parallel
for (int i = 0; i < ARRAY_SIZE; i++)
{
func(a, b, c, i);
}
#endif
printf("Time consumed is %.6f\n", mysecond() - start);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment