示例#1
0
  void * run_one_test(void * arg)
#endif
{
  list_element * t[MAX_NTHREADS + 1];
  int index = (int)(size_t)arg;
  int i;
# ifdef VERBOSE
    int j = 0;

    printf("starting thread %d\n", index);
# endif
  while (fetch_and_add(&ops_performed, index + 1) + index + 1 < LIMIT)
    {
      for (i = 0; i < index + 1; ++i)
        {
          t[i] = (list_element *)AO_stack_pop(&the_list);
          if (0 == t[i])
            {
              fprintf(stderr, "FAILED\n");
              abort();
            }
        }
      for (i = 0; i < index + 1; ++i)
        {
          AO_stack_push(&the_list, (AO_t *)t[i]);
        }
#     ifdef VERBOSE
        j += index + 1;
#     endif
    }
# ifdef VERBOSE
    printf("finished thread %d: %d total ops\n", index, j);
# endif
  return 0;
}
示例#2
0
int main(int argc, char **argv)
{
  int nthreads;
  int max_nthreads;
  int exper_n;

  if (1 == argc)
    max_nthreads = 4;
  else if (2 == argc)
    {
      max_nthreads = atoi(argv[1]);
      if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS)
        {
          fprintf(stderr, "Invalid max # of threads argument\n");
          exit(1);
        }
    }
  else
    {
      fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]);
      exit(1);
    }
  for (exper_n = 0; exper_n < N_EXPERIMENTS; ++ exper_n)
    for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
      {
        int i;
#       ifdef USE_WINTHREADS
          DWORD thread_id;
          HANDLE thread[MAX_NTHREADS];
#       else
          pthread_t thread[MAX_NTHREADS];
#       endif
        int list_length = nthreads*(nthreads+1)/2;
        long long start_time;
        list_element * le;

#       ifdef VERBOSE
          printf("Before add_elements: exper_n=%d, nthreads=%d,"
                 " max_nthreads=%d, list_length=%d\n",
                 exper_n, nthreads, max_nthreads, list_length);
#       endif
        add_elements(list_length);
#       ifdef VERBOSE
          printf("Initial list (nthreads = %d):\n", nthreads);
          print_list();
#       endif
        ops_performed = 0;
        start_time = get_msecs();
        for (i = 1; i < nthreads; ++i) {
          int code;

#         ifdef USE_WINTHREADS
            thread[i] = CreateThread(NULL, 0, run_one_test, (LPVOID)(size_t)i,
                                     0, &thread_id);
            code = thread[i] != NULL ? 0 : (int)GetLastError();
#         else
            code = pthread_create(&thread[i], 0, run_one_test,
                                  (void *)(size_t)i);
#         endif
          if (code != 0) {
            fprintf(stderr, "Thread creation failed %u\n", (unsigned)code);
            exit(3);
          }
        }
        /* We use the main thread to run one test.  This allows gprof   */
        /* profiling to work, for example.                              */
        run_one_test(0);
        for (i = 1; i < nthreads; ++i) {
          int code;

#         ifdef USE_WINTHREADS
            code = WaitForSingleObject(thread[i], INFINITE) == WAIT_OBJECT_0 ?
                        0 : (int)GetLastError();
#         else
            code = pthread_join(thread[i], 0);
#         endif
          if (code != 0) {
            fprintf(stderr, "Thread join failed %u\n", (unsigned)code);
            abort();
          }
        }
        times[nthreads][exper_n] = (unsigned long)(get_msecs() - start_time);
  #     ifdef VERBOSE
          printf("%d %lu\n", nthreads,
                 (unsigned long)(get_msecs() - start_time));
          printf("final list (should be reordered initial list):\n");
          print_list();
  #     endif
        check_list(list_length);
        while ((le = (list_element *)AO_stack_pop(&the_list)) != 0)
          free(le);
      }
    for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
      {
#       ifndef NO_TIMES
          unsigned long sum = 0;
#       endif

        printf("About %d pushes + %d pops in %d threads:",
               LIMIT, LIMIT, nthreads);
#       ifndef NO_TIMES
          for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n) {
#           if defined(VERBOSE)
              printf(" [%lu]", times[nthreads][exper_n]);
#           endif
            sum += times[nthreads][exper_n];
          }
          printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS);
#       else
          printf(" completed\n");
#       endif
      }
  return 0;
}
示例#3
0
int main(int argc, char **argv)
{
  int nthreads;
  int max_nthreads;
  int exper_n;

  if (1 == argc)
    max_nthreads = 4;
  else if (2 == argc)
    {
      max_nthreads = atoi(argv[1]);
      if (max_nthreads < 1 || max_nthreads > MAX_NTHREADS)
        {
          fprintf(stderr, "Invalid max # of threads argument\n");
          exit(1);
        }
    }
  else
    {
      fprintf(stderr, "Usage: %s [max # of threads]\n", argv[0]);
      exit(1);
    }
  for (exper_n = 0; exper_n < N_EXPERIMENTS; ++ exper_n)
    for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
      {
        int i;
        pthread_t thread[MAX_NTHREADS];
        int list_length = nthreads*(nthreads+1)/2;
        long long start_time;
        list_element * le;

        add_elements(list_length);
#       ifdef VERBOSE
          printf("Initial list (nthreads = %d):\n", nthreads);
          print_list();
#       endif
        ops_performed = 0;
        start_time = get_msecs();
        for (i = 1; i < nthreads; ++i) {
          int code;

          if ((code = pthread_create(thread+i, 0, run_one_test,
                                     (void *)(long)i)) != 0) {
            fprintf(stderr, "Thread creation failed %u\n", code);
            exit(3);
          }
        }
        /* We use the main thread to run one test.  This allows gprof   */
        /* profiling to work, for example.                              */
        run_one_test(0);
        for (i = 1; i < nthreads; ++i) {
          int code;
          if ((code = pthread_join(thread[i], 0)) != 0) {
            fprintf(stderr, "Thread join failed %u\n", code);
            abort();
          }
        }
        times[nthreads][exper_n] = (unsigned long)(get_msecs() - start_time);
  #     ifdef VERBOSE
          printf("%d %lu\n", nthreads,
                 (unsigned long)(get_msecs() - start_time));
          printf("final list (should be reordered initial list):\n");
          print_list();
  #     endif
        check_list(list_length);
        while ((le = (list_element *)AO_stack_pop(&the_list)) != 0)
          free(le);
      }
# ifndef NO_TIMES
    for (nthreads = 1; nthreads <= max_nthreads; ++nthreads)
      {
        unsigned long sum = 0;

        printf("About %d pushes + %d pops in %d threads:",
               LIMIT, LIMIT, nthreads);
        for (exper_n = 0; exper_n < N_EXPERIMENTS; ++exper_n)
          {
#           if defined(VERBOSE)
              printf("[%lu] ", times[nthreads][exper_n]);
#           endif
            sum += times[nthreads][exper_n];
          }
        printf(" %lu msecs\n", (sum + N_EXPERIMENTS/2)/N_EXPERIMENTS);
      }
# endif /* !NO_TIMES */
  return 0;
}