Révision 26
| Pi/C/Pthreads/Pi_Pthreads.c (revision 26) | ||
|---|---|---|
| 13 | 13 |
#include <stdlib.h> |
| 14 | 14 |
#include <math.h> |
| 15 | 15 |
#include <stdio.h> |
| 16 |
#include <limits.h> |
|
| 16 | 17 |
|
| 17 | 18 |
#define NUM_THREADS 1024 |
| 18 | 19 |
|
| 19 | 20 |
#define ITERATIONS 1000000000 |
| 20 | 21 |
|
| 21 | 22 |
#ifdef LONG |
| 22 |
#define LENGTH unsigned long
|
|
| 23 |
#define LENGTH long long
|
|
| 23 | 24 |
#else |
| 24 |
#define LENGTH unsigned int
|
|
| 25 |
#define LENGTH int |
|
| 25 | 26 |
#endif |
| 26 | 27 |
|
| 27 | 28 |
struct thread_data |
| ... | ... | |
| 82 | 83 |
z = my_data->seed_z; |
| 83 | 84 |
w = my_data->seed_w; |
| 84 | 85 |
|
| 85 |
printf("\tThread #%i, with seeds (%u,%u) and %lu !\n",
|
|
| 86 |
taskid,z,w,(unsigned long)iterations);
|
|
| 86 |
printf("\tThread #%i, with seeds (%u,%u) and %lld !\n",
|
|
| 87 |
taskid,z,w,(long long)iterations);
|
|
| 87 | 88 |
|
| 88 | 89 |
for (LENGTH i=0;i<iterations;i++) {
|
| 89 | 90 |
|
| ... | ... | |
| 121 | 122 |
printf("\t\t#1 : number of iterations (default 1 billion)\n");
|
| 122 | 123 |
printf("\t\t#2 : number of threads (default 1)\n\n");
|
| 123 | 124 |
} |
| 124 |
|
|
| 125 |
|
|
| 126 |
printf ("Sizeof int = %d bytes.\n", sizeof(int));
|
|
| 127 |
printf ("Sizeof long = %d bytes.\n", sizeof(long));
|
|
| 128 |
printf ("Sizeof long long = %d bytes.\n", sizeof(long long));
|
|
| 129 |
|
|
| 130 |
printf ("Max int = %u\n", INT_MAX);
|
|
| 131 |
printf ("Max long = %ld\n", LONG_MAX);
|
|
| 132 |
printf ("Max long long = %lld\n", LLONG_MAX);
|
|
| 133 |
|
|
| 125 | 134 |
printf("\tNumber of threads defined to %u\n",num_threads);
|
| 126 |
printf("\tNumber of iterations defined to %lu\n\n",(unsigned long)iterations);
|
|
| 135 |
printf("\tNumber of iterations defined to %lld\n\n",(long long)iterations);
|
|
| 127 | 136 |
|
| 128 | 137 |
/* Initialize and set thread detached attribute */ |
| 129 | 138 |
pthread_attr_init(&attr); |
| ... | ... | |
| 132 | 141 |
for(t=0;t<num_threads;t++) {
|
| 133 | 142 |
|
| 134 | 143 |
thread_data_array[t].thread_id = t; |
| 135 |
thread_data_array[t].iterations = iterations/num_threads; |
|
| 144 |
thread_data_array[t].iterations = iterations/(long long)num_threads;
|
|
| 136 | 145 |
thread_data_array[t].seed_w = (t+1)<<4; |
| 137 | 146 |
thread_data_array[t].seed_z = (1048576*(t+1))>>4; |
| 138 | 147 |
thread_data_array[t].inside = 0; |
| ... | ... | |
| 161 | 170 |
} |
| 162 | 171 |
|
| 163 | 172 |
for(t=0;t<num_threads;t++) {
|
| 164 |
printf("\tReturn to main with %i thread, %lu inside\n",
|
|
| 165 |
t,(unsigned long)thread_data_array[t].inside);
|
|
| 173 |
printf("\tReturn to main with %i thread, %lld inside\n",
|
|
| 174 |
t,(long long)thread_data_array[t].inside);
|
|
| 166 | 175 |
inside+=thread_data_array[t].inside; |
| 167 | 176 |
} |
| 168 | 177 |
|
| ... | ... | |
| 170 | 179 |
|
| 171 | 180 |
pi=4.*(float)inside/(float)iterations; |
| 172 | 181 |
|
| 173 |
printf("\tPi=%f with error %f and %lu iterations\n\n",pi,
|
|
| 174 |
fabs(pi-4*atan(1))/pi,(unsigned long)iterations);
|
|
| 182 |
printf("\tPi=%f with error %f and %lld iterations\n\n",pi,
|
|
| 183 |
fabs(pi-4*atan(1))/pi,(long long)iterations);
|
|
| 175 | 184 |
|
| 176 | 185 |
pthread_exit(NULL); |
| 177 | 186 |
} |
Formats disponibles : Unified diff