Commit 6c373d6659f2b628fe038bee1e9b901b704f2342

Authored by Sathya Narayanan
1 parent 9b5553a5a4
Exists in master

Correct code for memory acess time

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

memory_access_time.c View file @ 6c373d6
#include<stdio.h> 1 1 #include<stdio.h>
#include <sys/time.h> 2 2 #include <sys/time.h>
#include <stdlib.h> 3 3 #include <stdlib.h>
4 4
#define KB 1024 5 5 #define KB 1024
#define MB 1024 * KB 6 6 #define MB 1024 * KB
#define MAX_ARRAY_SIZE 32*MB 7 7 #define MAX_ARRAY_SIZE 32*MB
#define STRIDE_SIZE 16 8 8 #define STRIDE_SIZE 16
#define CPU_FREQ 900 9 9 #define CPU_FREQ 900
10 10
typedef unsigned long long int u64; 11 11 typedef unsigned long long int u64;
12 12
u64 access_time_1(u64 size, int stride_size){ 13 13 u64 access_time_1(u64 size, int stride_size){
struct timeval start, end; 14 14 struct timeval start, end;
struct timezone tz; 15 15 struct timezone tz;
16 16
int *array = (int *)malloc(size); 17 17 int *array = (int *)malloc(size);
int len = (size)/sizeof(int); 18 18 int len = (size)/sizeof(int);
unsigned int i; 19 19 unsigned int i;
20 20
gettimeofday(&start, &tz); 21 21 gettimeofday(&start, &tz);
for(i=0; i < MAX_ARRAY_SIZE; i++) { 22 22 for(i=0; i < MAX_ARRAY_SIZE; i++) {
array[( i * stride_size) % len]++; 23 23 array[( i * stride_size) % len]++;
} 24 24 }
gettimeofday(&end, &tz); 25 25 gettimeofday(&end, &tz);
26 26
free(array); 27 27 free(array);
return ((end.tv_sec * 1e6) + end.tv_usec - (start.tv_sec * 1e6) - start.tv_usec); 28 28 return ((end.tv_sec * 1e6) + end.tv_usec - (start.tv_sec * 1e6) - start.tv_usec);
} 29 29 }
30 30
int main() 31 31 int main()
{ 32 32 {
int i, j; 33 33 int i, j;
34 34
for (j = 4; j <= 32; j*=2) { 35 35 for (j = 4; j <= 256; j*=2) {
printf("Stride size: %u\n", j); 36 36 printf("Stride size: %u\n", j);
for (i = 1024; i <= 32 * MB; i*=2) { 37 37 for (i = 1024; i <= 32 * MB; i*=2) {
u64 access_time = access_time_1(i, j); 38 38 u64 access_time = access_time_1(i, j);