int main(int argc, char *argv[]) { pthread_t *threads; ck_barrier_tournament_round_t **rounds; int i; unsigned int size; if (argc < 3) { fprintf(stderr, "Usage: correct <number of threads> <affinity delta>\n"); exit(EXIT_FAILURE); } nthr = atoi(argv[1]); if (nthr <= 0) { fprintf(stderr, "ERROR: Number of threads must be greater than 0\n"); exit(EXIT_FAILURE); } a.delta = atoi(argv[2]); threads = malloc(sizeof(pthread_t) * nthr); if (threads == NULL) { fprintf(stderr, "ERROR: Could not allocate thread structures\n"); exit(EXIT_FAILURE); } rounds = malloc(sizeof(ck_barrier_tournament_round_t *) * nthr); if (rounds == NULL) { fprintf(stderr, "ERROR: Could not allocate barrier structures\n"); exit(EXIT_FAILURE); } size = ck_barrier_tournament_size(nthr); for (i = 0; i < nthr; ++i) { rounds[i] = malloc(sizeof(ck_barrier_tournament_round_t) * size); if (rounds[i] == NULL) { fprintf(stderr, "ERROR: Could not allocate barrier structures\n"); exit(EXIT_FAILURE); } } ck_barrier_tournament_init(&barrier, rounds, nthr); fprintf(stderr, "Creating threads (barrier)..."); for (i = 0; i < nthr; i++) { if (pthread_create(&threads[i], NULL, thread, NULL)) { fprintf(stderr, "ERROR: Could not create thread %d\n", i); exit(EXIT_FAILURE); } } fprintf(stderr, "done\n"); fprintf(stderr, "Waiting for threads to finish correctness regression..."); for (i = 0; i < nthr; i++) pthread_join(threads[i], NULL); fprintf(stderr, "done (passed)\n"); return (0); }
void ck_barrier_tournament_init(struct ck_barrier_tournament *barrier, struct ck_barrier_tournament_round **rounds, unsigned int nthr) { unsigned int i, k, size, twok, twokm1, imod2k; ck_pr_store_uint(&barrier->tid, 0); size = ck_barrier_tournament_size(nthr); for (i = 0; i < nthr; ++i) { /* The first role is always CK_BARRIER_TOURNAMENT_DROPOUT. */ rounds[i][0].flag = 0; rounds[i][0].role = CK_BARRIER_TOURNAMENT_DROPOUT; for (k = 1, twok = 2, twokm1 = 1; k < size; ++k, twokm1 = twok, twok <<= 1) { rounds[i][k].flag = 0; imod2k = i & (twok - 1); if (imod2k == 0) { if ((i + twokm1 < nthr) && (twok < nthr)) rounds[i][k].role = CK_BARRIER_TOURNAMENT_WINNER; else if (i + twokm1 >= nthr) rounds[i][k].role = CK_BARRIER_TOURNAMENT_BYE; } if (imod2k == twokm1) rounds[i][k].role = CK_BARRIER_TOURNAMENT_LOSER; else if ((i == 0) && (twok >= nthr)) rounds[i][k].role = CK_BARRIER_TOURNAMENT_CHAMPION; if (rounds[i][k].role == CK_BARRIER_TOURNAMENT_LOSER) rounds[i][k].opponent = &rounds[i - twokm1][k].flag; else if (rounds[i][k].role == CK_BARRIER_TOURNAMENT_WINNER || rounds[i][k].role == CK_BARRIER_TOURNAMENT_CHAMPION) rounds[i][k].opponent = &rounds[i + twokm1][k].flag; } } ck_pr_store_ptr(&barrier->rounds, rounds); return; }