Beispiel #1
0
int main(void)
{
  // create memory system with 256 words and a direct-mapped cache
  // with 8 sets and a block size of 4 words.
  
  void *a = initializeMemorySystem(SIZE*SIZE, 1, 4, 8, 1);
  if (a == NULL)
  {
    fprintf(stderr, "initializeMemorySystem failed!\n");
    exit(-1);
  }

  // simulate two-dimensional array access in row-major order
  int i, j, sum;
  sum = 0;
  for (i = 0; i < SIZE; i++)
    for (j = 0; j < SIZE; j++)
      sum += readInt(a, 0, i*SIZE+j);

  // print stats
  printf("First row-major order:\n");
  printStatistics(a);
  printf("\n");

  // re-create the memory system
  a = initializeMemorySystem(SIZE*SIZE, 1, 4, 8, 1);
  if (a == NULL)
  {
    fprintf(stderr, "initializeMemorySystem failed!\n");
    exit(-1);
  }

  // simulate two-dimensional array access in column-major order
  sum = 0;
  for (i = 0; i < SIZE; i++)
    for (j = 0; j < SIZE; j++)
      sum += readInt(a, 0, j*SIZE+i);

  // print stats
  printf("Second column-major order:\n");
  printStatistics(a);
  printf("\n");

  return 0;
}
Beispiel #2
0
int main(void)
{
  // create memory system with 256 words and a direct-mapped cache
  // with 8 sets and a block size of 4 words.
  void *a = initializeMemorySystem(SIZE, 1, 4, 8, 8, 1);
  if (a == NULL)
  {
    fprintf(stderr, "initializeMemorySystem failed!\n");
    exit(-1);
  }

  int i, sum, sum2;

  // initialize the array
  for (i = 0; i < SIZE; i++)
  {
    writeInt(a, 0, i, i);
  }

  // now sum it
  sum = 0;
  sum2 = 0;
  for (i = 0; i < SIZE; i++)
  {
    int tmp = readInt(a, 0, i);
    //printf("# %d %d\n", tmp, i);
    sum += tmp;
    sum2 += i;
  }
  
  //added to test multiple lines
  //*******************************************
  sum = 0;
  sum2 = 0;
  for (i = 0; i < SIZE; i++)
  {
    int tmp = readInt(a, 0, i);
    // printf("# %d %d\n", tmp, i);
    sum += tmp;
    sum2 += i;
  }
  //********************************************
  
  printf("sum is %d (should be %d)\n", sum, sum2);

  // print stats
  printf("\n");
  printStatistics(a);
  printf("\n");


  return 0;
}
Beispiel #3
0
int main(void)
{
  int i;

  // create memory system with 256 words and NUM_THREADS caches
  // with 8 sets, N lines per set and a block size of 4 words.
  memSystem = initializeMemorySystem(SIZE, NUM_THREADS, 4, 8, N, NUM_LOCKS);
  if (memSystem == NULL)
  {
    fprintf(stderr, "initializeMemorySystem failed!\n");
    exit(-1);
  }

  if (pthread_mutex_init(&mu, NULL) != 0)
    error("can't init mutex");

  // now create NUM_THREADS threads to increment on location in the cache
  for (i=0; i < NUM_THREADS; i++)
  {
    // create threads; DANGER: thread logical id (int) passed as "void *"
    if (pthread_create(&pt[i], NULL, work, (void *) i) != 0)
      error("error in thread create");
  }

  // wait for threads to finish
  for (i=0; i < NUM_THREADS; i++)
  {
    if (pthread_join(pt[i], NULL))
    {
      error("error in thread join");
    }
  }

  printf("answer is %d (should be %d)\n",
    readInt(memSystem, 0, ADDRESS),
    INC * NUM_THREADS);


  // print stats
  printf("\n");
  printStatistics(memSystem);
  printf("\n");


  return 0;
}
Beispiel #4
0
int main(void)
{
  // create memory system with 256 words and a direct-mapped cache
  // with 8 sets and a block size of 4 words.
  void *a = initializeMemorySystem(SIZE, 1, 4, 8, 1, 1);
  if (a == NULL)
  {
    fprintf(stderr, "initializeMemorySystem failed!\n");
    exit(-1);
  }

  int i;

  // initialize the array
  for (i = 0; i < SIZE; i++)
  {
    writeFloat(a, 0, i, ((float) i) + .5);
  }

  // now sum it
  float sum = 0.0;
  float sum2 = 0.0;
  for (i = 0; i < SIZE; i++)
  {
    float tmp = readFloat(a, 0, i);
    //printf("%d %f\n", i, tmp);
    sum += tmp;
    sum2 += (((float) i) + .5);
  }
  printf("sum is %f (should be %f)\n", sum, sum2);

  // print stats
  printf("\n");
  printStatistics(a);
  printf("\n");


  return 0;
}