1 | // $Id: main.c 139 2010-07-30 14:47:27Z rosiere $ |
---|
2 | |
---|
3 | /* |
---|
4 | * All thread execute this routine |
---|
5 | * Initialize the thread and attribute a Workload at each thread |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdlib.h> |
---|
9 | #include <stdio.h> |
---|
10 | |
---|
11 | #define TEST_ERROR |
---|
12 | |
---|
13 | #define NB_ITERATION 1000 |
---|
14 | |
---|
15 | //=====[ my_rand ]============================================================== |
---|
16 | |
---|
17 | |
---|
18 | static unsigned long my_rand_next = 1; |
---|
19 | |
---|
20 | #define MY_RAND_MAX 32768 |
---|
21 | |
---|
22 | int my_rand(void) |
---|
23 | { |
---|
24 | my_rand_next = my_rand_next * 1103515245 + 12345; |
---|
25 | return((unsigned)(my_rand_next/65536) % MY_RAND_MAX); |
---|
26 | } |
---|
27 | |
---|
28 | void my_srand(unsigned grain) |
---|
29 | { |
---|
30 | my_rand_next = grain; |
---|
31 | } |
---|
32 | |
---|
33 | //=====[ test ]================================================================= |
---|
34 | |
---|
35 | void test_take (int x, int * take, int * not_take) |
---|
36 | { |
---|
37 | if ((x&1) == 1) // !!!! |
---|
38 | (*take) ++; |
---|
39 | else |
---|
40 | (*not_take) ++; |
---|
41 | } |
---|
42 | |
---|
43 | void test_no_take (int x, int * take, int * not_take) |
---|
44 | { |
---|
45 | if ((x&1) != 0) // !!!! |
---|
46 | (*take) ++; |
---|
47 | else |
---|
48 | (*not_take) ++; |
---|
49 | } |
---|
50 | |
---|
51 | //=====[ f ]==================================================================== |
---|
52 | |
---|
53 | int f (int nb_iteration) |
---|
54 | { |
---|
55 | #ifdef TEST_ERROR |
---|
56 | #define NB_VALUE 1000 |
---|
57 | |
---|
58 | const int res_my_rand [NB_VALUE] = // to grain = 1 |
---|
59 | { |
---|
60 | 16838, 5758, 10113, 17515, 31051, 5627, 23010, 7419, 16212, 4086, |
---|
61 | 2749, 12767, 9084, 12060, 32225, 17543, 25089, 21183, 25137, 25566, |
---|
62 | 26966, 4978, 20495, 10311, 11367, 30054, 17031, 13145, 19882, 25736, |
---|
63 | 30524, 28505, 28394, 22102, 24851, 19067, 12754, 11653, 6561, 27096, |
---|
64 | 13628, 15188, 32085, 4143, 6967, 31406, 24165, 13403, 25562, 24834, |
---|
65 | 31353, 920, 10444, 24803, 7962, 19318, 1422, 31327, 10457, 1945, |
---|
66 | 14479, 29983, 18751, 3894, 18670, 8259, 16248, 7757, 15629, 13306, |
---|
67 | 28606, 13990, 11738, 12516, 1414, 5262, 17116, 22825, 3181, 13134, |
---|
68 | 25343, 8022, 11233, 7536, 9760, 9979, 29071, 1201, 21336, 13061, |
---|
69 | 22160, 24005, 30729, 7644, 27475, 31693, 25514, 14139, 22088, 26521, |
---|
70 | |
---|
71 | 5202, 9171, 4434, 28317, 24582, 6815, 4586, 9653, 26306, 7174, |
---|
72 | 18451, 23448, 6473, 32434, 8193, 14110, 24748, 28210, 29320, 32049, |
---|
73 | 12956, 14162, 4166, 14997, 7793, 32310, 21391, 19799, 7926, 14905, |
---|
74 | 25885, 2582, 15610, 5000, 8052, 30965, 20120, 32380, 15639, 26204, |
---|
75 | 24385, 12475, 15725, 17265, 3214, 19471, 11376, 4697, 25543, 23297, |
---|
76 | 14619, 23087, 3123, 31549, 18065, 24256, 18973, 20901, 25613, 6157, |
---|
77 | 9899, 9267, 22413, 9598, 18526, 13711, 10046, 14566, 18536, 15988, |
---|
78 | 19878, 13626, 4273, 8387, 1171, 32017, 3752, 12388, 21191, 11483, |
---|
79 | 18122, 11744, 18528, 15585, 5363, 20159, 5641, 18176, 9575, 28578, |
---|
80 | 27363, 27685, 29344, 19489, 17713, 5511, 21461, 22626, 8645, 3496, |
---|
81 | |
---|
82 | 26703, 6270, 13870, 11529, 27499, 4500, 8607, 5808, 15725, 12457, |
---|
83 | 16542, 16474, 11531, 17222, 3952, 17024, 19894, 24015, 18247, 11276, |
---|
84 | 26278, 19365, 8746, 21976, 18092, 25851, 29088, 29163, 2231, 26233, |
---|
85 | 29732, 21106, 5411, 9874, 5448, 9344, 27589, 17574, 1191, 6789, |
---|
86 | 695, 11735, 20364, 17040, 17892, 5035, 26979, 1092, 850, 12390, |
---|
87 | 20195, 668, 20531, 29989, 12281, 23902, 12970, 32186, 19571, 3680, |
---|
88 | 7261, 26187, 28529, 24190, 446, 24233, 13708, 11863, 6681, 6001, |
---|
89 | 2499, 3786, 5214, 25829, 1322, 25907, 19628, 13192, 7505, 5990, |
---|
90 | 20129, 10875, 19829, 31591, 12388, 6042, 9833, 1775, 4719, 342, |
---|
91 | 28994, 31392, 20509, 31313, 20677, 1282, 11511, 4795, 3474, 6469, |
---|
92 | |
---|
93 | 2750, 879, 30989, 30134, 29752, 28364, 4880, 5629, 2235, 21332, |
---|
94 | 24145, 3356, 5243, 3079, 3988, 807, 24979, 31357, 914, 21187, |
---|
95 | 3540, 14022, 10149, 609, 29009, 24833, 16696, 5432, 24999, 28863, |
---|
96 | 16369, 28676, 24077, 7701, 1691, 19840, 28703, 16515, 22229, 32420, |
---|
97 | 19817, 16264, 19324, 29343, 1462, 28929, 3546, 17043, 18967, 325, |
---|
98 | 12683, 15634, 6322, 16642, 25395, 11612, 22864, 29910, 21985, 23126, |
---|
99 | 13988, 685, 6978, 31050, 16476, 6365, 21126, 4193, 8681, 11011, |
---|
100 | 23058, 1249, 31247, 24731, 28650, 20774, 7980, 20833, 27868, 7778, |
---|
101 | 23624, 11115, 1645, 15892, 29408, 7939, 17285, 16202, 28018, 11334, |
---|
102 | 4058, 7062, 3784, 11901, 6684, 14289, 27141, 16702, 26853, 13458, |
---|
103 | |
---|
104 | 28528, 23363, 21087, 19052, 31235, 15109, 17075, 11755, 10675, 288, |
---|
105 | 32053, 14157, 5758, 5222, 17488, 18945, 10294, 11200, 5171, 14305, |
---|
106 | 7951, 6601, 23608, 7214, 6377, 13865, 25369, 27215, 8030, 177, |
---|
107 | 16849, 11337, 2699, 23099, 8531, 11517, 17567, 28479, 9966, 2597, |
---|
108 | 14885, 12341, 15227, 27149, 785, 29615, 6476, 20753, 4236, 7730, |
---|
109 | 19668, 21210, 27519, 27608, 5142, 6999, 20449, 14246, 18638, 2941, |
---|
110 | 12481, 23726, 16738, 26047, 28947, 3300, 21639, 17996, 23866, 14785, |
---|
111 | 27571, 25356, 12633, 27289, 20551, 20312, 14426, 7357, 8056, 16252, |
---|
112 | 20410, 2384, 4353, 2029, 23579, 27882, 31882, 21577, 31368, 11502, |
---|
113 | 18902, 21012, 31365, 30379, 14256, 19244, 27870, 13365, 9619, 27665, |
---|
114 | |
---|
115 | 21293, 29441, 19484, 12759, 15400, 6211, 4144, 15335, 22704, 32520, |
---|
116 | 23789, 32121, 21913, 23571, 12369, 2770, 1594, 887, 18093, 32317, |
---|
117 | 11188, 27640, 4295, 12490, 25859, 3106, 24786, 17097, 5062, 27943, |
---|
118 | 31247, 12292, 16846, 3962, 28479, 27584, 21352, 32044, 31695, 16498, |
---|
119 | 4714, 9737, 17346, 28482, 24064, 24946, 9822, 19265, 2868, 12792, |
---|
120 | 3990, 19561, 16959, 9917, 29754, 22104, 25745, 30347, 25825, 11980, |
---|
121 | 18338, 12465, 25490, 24541, 20599, 8097, 26677, 510, 15347, 14667, |
---|
122 | 16316, 10673, 8163, 14647, 12817, 29965, 28871, 16485, 6846, 29937, |
---|
123 | 6241, 5861, 24670, 28166, 26879, 27445, 30407, 11638, 31113, 11101, |
---|
124 | 22516, 28014, 13898, 21452, 8117, 1364, 12926, 20465, 13845, 25032, |
---|
125 | |
---|
126 | 1696, 18585, 13455, 92, 18288, 31755, 29167, 27412, 25030, 31717, |
---|
127 | 24842, 28572, 26393, 27828, 11805, 3660, 5838, 9046, 18182, 23772, |
---|
128 | 12354, 15377, 14885, 22759, 22934, 1903, 6159, 26145, 10045, 26847, |
---|
129 | 17401, 29783, 4167, 781, 24287, 2347, 23402, 19964, 14711, 10637, |
---|
130 | 20693, 29417, 30937, 24313, 15541, 2528, 23019, 21618, 3716, 5402, |
---|
131 | 16270, 18242, 8468, 22045, 30602, 18286, 30559, 22674, 25146, 1019, |
---|
132 | 31243, 20197, 26870, 7908, 448, 7327, 28485, 17622, 14890, 10883, |
---|
133 | 10943, 1201, 10701, 24682, 3524, 8895, 27276, 24798, 12511, 25522, |
---|
134 | 24311, 25209, 26359, 2912, 28759, 17148, 26683, 2877, 8571, 12334, |
---|
135 | 16331, 7324, 577, 635, 26663, 17061, 20451, 9544, 16984, 28693, |
---|
136 | |
---|
137 | 19975, 10519, 21472, 19641, 9349, 23391, 30563, 22184, 19958, 41, |
---|
138 | 27666, 23363, 25344, 2410, 28635, 1006, 20068, 26660, 20562, 29323, |
---|
139 | 13764, 21307, 7773, 10683, 9013, 8216, 28834, 15571, 15241, 2303, |
---|
140 | 24754, 928, 3417, 32048, 12018, 19931, 17621, 22209, 14669, 21098, |
---|
141 | 20555, 4662, 12444, 22109, 16760, 25574, 14544, 18339, 30610, 18189, |
---|
142 | 24549, 18320, 24081, 25264, 6022, 24847, 19521, 24628, 7552, 26278, |
---|
143 | 20873, 4029, 32489, 13359, 31352, 30279, 8717, 20560, 23189, 8583, |
---|
144 | 795, 10687, 26339, 23935, 3988, 27703, 27958, 10212, 19567, 10553, |
---|
145 | 13368, 32047, 13068, 20611, 4154, 23429, 28895, 13393, 30908, 5960, |
---|
146 | 30169, 5462, 27323, 23929, 32149, 29305, 9149, 32724, 32584, 6224, |
---|
147 | |
---|
148 | 413, 26281, 12389, 11357, 4887, 17532, 1555, 12299, 9490, 26460, |
---|
149 | 24911, 14183, 22704, 17412, 20961, 20382, 3608, 5811, 7168, 20794, |
---|
150 | 12286, 13771, 27844, 6684, 11255, 29227, 26960, 27448, 9086, 8526, |
---|
151 | 1211, 26131, 8645, 11552, 18627, 26110, 25149, 17828, 32048, 12559, |
---|
152 | 29668, 4167, 23050, 15588, 17746, 28929, 12410, 791, 25985, 18737, |
---|
153 | 13952, 8576, 1375, 18485, 24922, 16941, 1557, 10700, 31408, 1826, |
---|
154 | 8213, 32301, 23680, 30161, 28390, 22500, 32716, 7768, 28079, 31599, |
---|
155 | 18949, 31357, 12421, 27804, 17717, 16490, 26228, 30112, 12016, 9512, |
---|
156 | 28751, 1015, 2553, 19164, 6200, 30464, 7859, 22212, 13806, 15444, |
---|
157 | 5710, 27377, 19417, 21994, 7341, 32706, 26839, 9707, 13593, 22820, |
---|
158 | |
---|
159 | 23236, 7254, 11688, 5003, 32203, 8788, 23915, 1199, 13424, 24408, |
---|
160 | 6490, 27655, 21593, 13310, 3072, 12038, 5087, 1697, 23526, 7448, |
---|
161 | 17497, 15802, 19649, 30555, 25891, 1020, 18267, 14366, 23408, 12848, |
---|
162 | 29762, 22042, 23252, 31440, 21810, 32626, 19088, 13158, 2370, 2156, |
---|
163 | 13213, 22434, 10193, 2541, 887, 8766, 24112, 27574, 12206, 22418, |
---|
164 | 15373, 22113, 1582, 7670, 23202, 18714, 13897, 32731, 19105, 20253, |
---|
165 | 24201, 29608, 29918, 4338, 11064, 1366, 22970, 15723, 20907, 32202, |
---|
166 | 23379, 1626, 29779, 9206, 32365, 11539, 15233, 29614, 19999, 23205, |
---|
167 | 12028, 26186, 20029, 16343, 7237, 10497, 10377, 14564, 5540, 23808, |
---|
168 | 152, 28918, 22011, 15693, 6790, 16740, 5446, 18015, 16473, 18604 |
---|
169 | }; |
---|
170 | #endif |
---|
171 | |
---|
172 | #ifdef TEST_ERROR |
---|
173 | if (nb_iteration>NB_VALUE) |
---|
174 | { |
---|
175 | printf("Invalid iteration number.\n"); |
---|
176 | exit(1); |
---|
177 | } |
---|
178 | #endif |
---|
179 | |
---|
180 | my_srand(1); |
---|
181 | |
---|
182 | int take = 0; |
---|
183 | int not_take = 0; |
---|
184 | |
---|
185 | for (int i=0; i<nb_iteration; ++i) |
---|
186 | { |
---|
187 | int x = my_rand(); |
---|
188 | #ifdef TEST_ERROR |
---|
189 | int y = res_my_rand [i]; |
---|
190 | |
---|
191 | if (x != y) |
---|
192 | { |
---|
193 | printf("Invalid value\n"); |
---|
194 | exit(2); |
---|
195 | } |
---|
196 | #endif |
---|
197 | |
---|
198 | test_take (x, &take, ¬_take); |
---|
199 | test_no_take(x, &take, ¬_take); |
---|
200 | } |
---|
201 | |
---|
202 | #ifdef TEST_ERROR |
---|
203 | if ((take+not_take)!=(nb_iteration<<1)) |
---|
204 | { |
---|
205 | printf("Invalid sum\n"); |
---|
206 | exit(3); |
---|
207 | } |
---|
208 | #endif |
---|
209 | |
---|
210 | return take>>1; |
---|
211 | } |
---|
212 | |
---|
213 | //=====[ main ]================================================================= |
---|
214 | |
---|
215 | int main() |
---|
216 | { |
---|
217 | int take = 0; |
---|
218 | |
---|
219 | take+=f(NB_ITERATION); |
---|
220 | |
---|
221 | printf("Result : %d/%d\n",take,NB_ITERATION); |
---|
222 | |
---|
223 | exit (0); |
---|
224 | |
---|
225 | while (1); |
---|
226 | } |
---|