~ubuntu-branches/ubuntu/hardy/openmpi/hardy-updates

« back to all changes in this revision

Viewing changes to test/asm/atomic_math_noinline.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2006-10-15 00:46:11 UTC
  • Revision ID: james.westby@ubuntu.com-20061015004611-uuhxnaxyjmuxfd5h
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 
3
 *                         University Research and Technology
 
4
 *                         Corporation.  All rights reserved.
 
5
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 
6
 *                         of Tennessee Research Foundation.  All rights
 
7
 *                         reserved.
 
8
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 
 
9
 *                         University of Stuttgart.  All rights reserved.
 
10
 * Copyright (c) 2004-2005 The Regents of the University of California.
 
11
 *                         All rights reserved.
 
12
 * $COPYRIGHT$
 
13
 * 
 
14
 * Additional copyrights may follow
 
15
 * 
 
16
 * $HEADER$
 
17
 */
 
18
 
 
19
#define OMPI_BUILDING 0
 
20
#include "ompi_config.h"
 
21
 
 
22
#ifdef HAVE_PTHREAD_H
 
23
#include <pthread.h>
 
24
#endif
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
 
 
28
#include "opal/sys/atomic.h"
 
29
 
 
30
#define TEST_REPS 500
 
31
 
 
32
int32_t val32 = 0;
 
33
#if OPAL_HAVE_ATOMIC_MATH_64
 
34
int64_t val64 = 0;
 
35
#endif
 
36
int valint = 0;
 
37
 
 
38
static void* atomic_math_test(void* arg)
 
39
{
 
40
    int count = *((int*) arg);
 
41
    int i;
 
42
 
 
43
    for (i = 0 ; i < count ; ++i) {
 
44
        opal_atomic_add_32(&val32, 5);
 
45
#if OPAL_HAVE_ATOMIC_MATH_64
 
46
        opal_atomic_add_64(&val64, 6);
 
47
#endif
 
48
        opal_atomic_add(&valint, 4);
 
49
    }
 
50
 
 
51
    return NULL;
 
52
}
 
53
 
 
54
 
 
55
static int
 
56
atomic_math_test_th(int count, int thr_count)
 
57
{
 
58
    int value;
 
59
#if OMPI_HAVE_POSIX_THREADS
 
60
    pthread_t *th;
 
61
    int tid, ret = 0;
 
62
 
 
63
    th = (pthread_t *) malloc(thr_count * sizeof(pthread_t));
 
64
    if (!th) { 
 
65
        perror("malloc"); 
 
66
        exit(EXIT_FAILURE); 
 
67
    }
 
68
 
 
69
    /* Ok to use a single instance of ip_union_t from the stack here
 
70
       because a) we're passing the same count to all threads, and b)
 
71
       we're waiting for all the threads to finish before leaving this
 
72
       function, so there's no race condition of the instance
 
73
       disappearing before the threads start. */
 
74
    value = count;
 
75
    for (tid = 0; tid < thr_count; tid++) {
 
76
        if (pthread_create(&th[tid], NULL, atomic_math_test, 
 
77
                           (void*) &value) != 0) {
 
78
            perror("pthread_create");
 
79
            exit(EXIT_FAILURE);
 
80
        }
 
81
    }
 
82
 
 
83
    /* -- wait for the thread set to finish -- */
 
84
    for (tid = 0; tid < thr_count; tid++) {
 
85
        void *thread_return;
 
86
 
 
87
        if (pthread_join(th[tid], &thread_return) != 0) {
 
88
            perror("pthread_join");
 
89
            exit(EXIT_FAILURE);
 
90
        }
 
91
    }
 
92
    free(th);
 
93
 
 
94
    return ret;
 
95
#else
 
96
    value = count;
 
97
    if (thr_count == 1) {
 
98
        atomic_math_test((void*) &value);
 
99
    } else {
 
100
        return 77;
 
101
    }
 
102
#endif
 
103
 
 
104
    return 0;
 
105
}
 
106
 
 
107
 
 
108
int
 
109
main(int argc, char *argv[])
 
110
{
 
111
    int ret = 77;
 
112
    int num_threads = 1;
 
113
 
 
114
    if (argc != 2) {
 
115
        printf("*** Incorrect number of arguments.  Skipping test\n");
 
116
        return 77;
 
117
    }
 
118
    num_threads = atoi(argv[1]);
 
119
 
 
120
    ret = atomic_math_test_th(TEST_REPS, num_threads);
 
121
    if (ret == 77) return ret;
 
122
    opal_atomic_mb();
 
123
    if (val32 != TEST_REPS * num_threads * 5) {
 
124
        printf("opal_atomic_add32 failed.  Expected %d, got %d.\n",
 
125
               TEST_REPS * num_threads * 5, val32);
 
126
        ret = 1;
 
127
    }
 
128
#if OPAL_HAVE_ATOMIC_MATH_64
 
129
    if (val64 != TEST_REPS * num_threads * 6) {
 
130
        /* Safe to case to (int) here because we know it's going to be
 
131
           a small value */
 
132
        printf("opal_atomic_add32 failed.  Expected %d, got %d.\n",
 
133
               TEST_REPS * num_threads * 6, (int) val64);
 
134
        ret = 1;
 
135
    }
 
136
#else
 
137
    printf("      * skipping 64 bit tests\n");
 
138
#endif
 
139
    if (valint != TEST_REPS * num_threads * 4) {
 
140
        printf("opal_atomic_add32 failed.  Expected %d, got %d.\n",
 
141
               TEST_REPS * num_threads * 4, valint);
 
142
        ret = 1;
 
143
    }
 
144
 
 
145
    return ret;
 
146
}