Creo en nosotros. Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 2 #include <pthread.h>3 #include <stdio.h>4 #include <stdlib.h>5 #include <unistd.h>6 #include <sys/syscall.h> 7 #include <sched.h> 8 9 #define FALSE_SHARING 110 #define ITER_MAX 111 #define DIM (21000)12 #define CPU_MAX 1613 static volatile int matrix[DIM][DIM];14 static volatile int result[CPU_MAX];15 static volatile int cpuUsed; 16 17 #define min(a, b) ((a) <= (b) ? (a) : (b))18 19 20 /** Variables annadidas **/21 #define P 422 static int impares_p[P];23 24 25 void *body(void *cpu_nr)26 {27 28 int i, j,p = *((int *)cpu_nr);29 int pStart = p * DIM/P;30 int pEnd = (p+1) * DIM/P;31 32 33 for(i = pStart; i < pEnd; i++)34  for(j = 0; j < DIM; j++)35  if((matrix[i][j] % 2) != 0)36  impares_p[p]++;37  return NULL;38 39 40 }41 42 int main(int argc, char **argv)43 {44  pthread_t thr [CPU_MAX];45  int nombre[CPU_MAX]; 46  int i, j, k;47  cpu_set_t set;48  int cpuCount;49  int odds = 0;50 51  if(argc != 2) {52  printf("Use: false_sharing NumberOfCPUs\n"); 53  goto exception; 54  } 55  /* Getting number of CPUs */56  if (0 > (cpuCount = (int)sysconf( _SC_NPROCESSORS_ONLN ))) {57  perror( "sysconf" );58  return -1;59  }60  if(cpuCount < (cpuUsed = atoi(argv[1]))) {61  printf("Not enough CPUs\n");62  exit(1);63  }64  printf("SMP with %d Cpus Available. \nWorking with %d cpus\n", cpuCount, cpuUsed);65  66  CPU_ZERO( &set );67  for(k = 0; k < cpuUsed; k++)68  CPU_SET( k, &set ); 69  if (0 > sched_setaffinity( 0, sizeof( cpu_set_t ), &set )) goto exception;70 71  for(i = 0; i < cpuUsed ; i++) {72  result[i] = 0;73  nombre[i] = i;74  }75  for(i = 0; i < DIM; i++) {76  for(j = 0; j < DIM; j++)77  matrix[i][j] = DIM*i + j;78  }79 80  for(i = 0; i < cpuUsed; i++) 81  if(pthread_create(&thr[i], NULL, body, &nombre[i])) goto exception;82  for(i = 0; i < cpuUsed; i++) 83  if(pthread_join(thr[i], NULL)) goto exception;84 85  for( i = 0; i < cpuUsed ; i++)86  odds += result[i];87 88  int impares=0;89  int p;90  for(p = 0; p < P; p++ )91  impares += impares_p[p];92 93  94  printf("Casillas impares : %d\n", DIM*DIM/2);95  printf("Casillas impares obtenidas: %d\n", impares/ITER_MAX);96  return 0;97  98 exception:99  perror("");100  return 1;101 }
Enlace
El enlace para compartir es: