"Fossies" - the Fresh Open Source Software Archive 
Member "laspack/examples/vectopt/vectopt.c" (8 Aug 1995, 9490 Bytes) of package /linux/privat/old/laspack.tgz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /****************************************************************************/
2 /* vectopt.c */
3 /****************************************************************************/
4 /* */
5 /* VECTor operation OPTimization for laspack */
6 /* */
7 /* Copyright (C) 1992-1995 Tomas Skalicky. All rights reserved. */
8 /* */
9 /****************************************************************************/
10 /* */
11 /* ANY USE OF THIS CODE CONSTITUTES ACCEPTANCE OF THE TERMS */
12 /* OF THE COPYRIGHT NOTICE (SEE FILE COPYRGHT.H) */
13 /* */
14 /****************************************************************************/
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <math.h>
21
22 #include <laspack/errhandl.h>
23 #include <laspack/vector.h>
24 #include <laspack/qmatrix.h>
25 #include <laspack/operats.h>
26 #include <laspack/version.h>
27 #include <laspack/copyrght.h>
28
29 #include <xc/getopts.h>
30
31 #include "testproc.h"
32
33 #define MAX_TEST_NR 10
34
35 static void PrintHelp(void);
36
37 int main(int argc, char **argv)
38 {
39 double Factor, ClockLASPack = 0.0, Clock[MAX_TEST_NR + 1];
40 int TestNo, NoCycles, Test, Cycle;
41 clock_t BegClock, EndClock;
42 size_t Dim;
43 Boolean Help;
44 Vector x, y;
45
46 OptTabType OptTab;
47
48 /* initial message */
49 fprintf(stderr, "vectopt Version %s\n", LASPACK_VERSION);
50 fprintf(stderr, " (C) 1992-1995 Tomas Skalicky\n");
51 fprintf(stderr, " Use option -h for help.\n");
52
53 /* generate options table */
54 OptTab.No = 3;
55 OptTab.Descr = (OptDescrType *)malloc(OptTab.No * sizeof(OptDescrType));
56 if (OptTab.Descr != NULL) {
57 /* boolean option */
58 OptTab.Descr[0].KeyChar = 'd';
59 OptTab.Descr[0].Type = SizeOptType;
60 OptTab.Descr[0].Variable = (void *)&Dim;
61
62 /* int option */
63 OptTab.Descr[1].KeyChar = 'c';
64 OptTab.Descr[1].Type = IntOptType;
65 OptTab.Descr[1].Variable = (void *)&NoCycles;
66
67 /* option for help */
68 OptTab.Descr[2].KeyChar = 'h';
69 OptTab.Descr[2].Type = BoolOptType;
70 OptTab.Descr[2].Variable = (void *)&Help;
71
72 /* initialize variables with default values */
73 #if defined(__MSDOS__) || defined(MSDOS)
74 /* the most MS-DOS compilers use size_t of 16 bits size, which rescricts
75 arrays to maximal 65536 / 8 = 8192 double elements */
76 Dim = 1000;
77 #else
78 Dim = 10000;
79 #endif
80 NoCycles = 10;
81 Help = False;
82
83 /* analyse options in the command line */
84 GetOpts(&argc, argv, &OptTab);
85 if (OptResult() == OptOK && !Help) {
86 /* construction of test vectors */
87 V_Constr(&x, "x", Dim, Normal, True);
88 V_Constr(&y, "y", Dim, Normal, True);
89
90 printf("\n");
91 printf("This program tests efficiency of several implementations\n");
92 printf("of the vector operation:\n\n");
93 printf(" <Vector 1> += <Vector 2>.\n");
94 fprintf(stderr, "\n");
95
96 Factor = 1.0 / CLOCKS_PER_SEC / NoCycles;
97
98 Test = 0;
99
100 /* implementation as in the current laspack version */
101
102 if (LASResult() == LASOK) {
103 fprintf(stderr, ".");
104 V_SetAllCmp(&x, 0.0);
105 V_SetAllCmp(&y, 0.0);
106
107 BegClock = clock();
108 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
109 AddAsgn_VV(&x, &y);
110 }
111 EndClock = clock();
112 ClockLASPack = (double)(EndClock - BegClock) * Factor;
113 }
114
115 /* implementation like laspack version 0.06 */
116
117 Test++;
118 if (LASResult() == LASOK) {
119 fprintf(stderr, ".");
120 V_SetAllCmp(&x, 0.0);
121 V_SetAllCmp(&y, 0.0);
122
123 BegClock = clock();
124 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
125 Test1_VV(&x, &y);
126 }
127 EndClock = clock();
128 Clock[Test] = (double)(EndClock - BegClock) * Factor;
129 }
130
131
132 /* implementation using local variables, ascended counting */
133
134 Test++;
135 if (LASResult() == LASOK) {
136 fprintf(stderr, ".");
137 V_SetAllCmp(&x, 0.0);
138 V_SetAllCmp(&y, 0.0);
139
140 BegClock = clock();
141 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
142 Test2_VV(&x, &y);
143 }
144 EndClock = clock();
145 Clock[Test] = (double)(EndClock - BegClock) * Factor;
146 }
147
148 /* implementation using local variables, descended counting */
149
150 Test++;
151 if (LASResult() == LASOK) {
152 fprintf(stderr, ".");
153 V_SetAllCmp(&x, 0.0);
154 V_SetAllCmp(&y, 0.0);
155
156 BegClock = clock();
157 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
158 Test3_VV(&x, &y);
159 }
160 EndClock = clock();
161 Clock[Test] = (double)(EndClock - BegClock) * Factor;
162 }
163
164 /* implementation using pointers, ascended counting */
165
166 Test++;
167 if (LASResult() == LASOK) {
168 fprintf(stderr, ".");
169 V_SetAllCmp(&x, 0.0);
170 V_SetAllCmp(&y, 0.0);
171
172 BegClock = clock();
173 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
174 Test4_VV(&x, &y);
175 }
176 EndClock = clock();
177 Clock[Test] = (double)(EndClock - BegClock) * Factor;
178 }
179
180 /* implementation using pointers, descended counting */
181
182 Test++;
183 if (LASResult() == LASOK) {
184 fprintf(stderr, ".");
185 V_SetAllCmp(&x, 0.0);
186 V_SetAllCmp(&y, 0.0);
187
188 BegClock = clock();
189 for(Cycle = 1; Cycle <= NoCycles; Cycle++) {
190 Test5_VV(&x, &y);
191 }
192 EndClock = clock();
193 Clock[Test] = (double)(EndClock - BegClock) * Factor;
194 }
195
196 fprintf(stderr, "\n");
197 TestNo = Test;
198
199 /* output of results */
200
201 if (LASResult() == LASOK) {
202 printf("\n");
203 printf("Results: (for dimension %lu, cycles %d)\n",
204 (unsigned long)Dim, NoCycles);
205 printf("--------\n\n");
206 printf(" implementation CPU time\n");
207 printf("-----------------------------------------\n");
208 if (Clock[Test] > 1e-5)
209 printf(" LASPack %12.3e s = 100.0 %%\n", ClockLASPack);
210 for (Test = 1; Test <= TestNo; Test++) {
211 if (Clock[Test] > 1e-5)
212 printf(" %2d %12.3e s = %5.1f %%\n",
213 Test, Clock[Test], 100.0 * Clock[Test] / ClockLASPack);
214 }
215 printf("-----------------------------------------\n\n");
216 printf("For details to the implementations look at the file\n");
217 printf("laspack/examples/vectopt/testproc.c.\n");
218 printf("The current LASPack version corresponds to the implementation %d.\n",
219 #if !defined(__hppa) && !defined(sparc)
220 2);
221 #else
222 3);
223 #endif
224 printf("This is, in comparison with LASPack, simplified and can be therefore\n");
225 printf("a little faster.\n");
226 }
227
228 /* LASPack error messages */
229 if (LASResult() != LASOK) {
230 fprintf(stderr, "\n");
231 fprintf(stderr, "LASPack error: ");
232 WriteLASErrDescr(stderr);
233 }
234
235 /* destruction of test vectors */
236 V_Destr(&x);
237 V_Destr(&y);
238 } else {
239 /* error messages */
240 if (OptResult() == OptNotDefErr || OptResult() == OptSyntaxErr || Help) {
241 fprintf(stderr, "\n");
242 PrintHelp();
243 }
244 if (OptResult() == OptDescrErr) {
245 fprintf(stderr, "\n");
246 fprintf(stderr, "Description of an option faulty.\n");
247 }
248 }
249
250 if (OptTab.Descr != NULL)
251 free(OptTab.Descr);
252 } else {
253 /* error message */
254 fprintf(stderr, "\n");
255 fprintf(stderr, "Not enought memory for analysis of command line options.\n");
256 }
257
258 return(0);
259 }
260
261 static void PrintHelp(void)
262 /* print help for syntax and avaible options */
263 {
264 fprintf(stderr, "Syntax:\n");
265 fprintf(stderr, " vectopt [-d<dim>] [-c<cycles>] [-h]\n\n");
266 fprintf(stderr, "Options:\n");
267 fprintf(stderr, " -d set vector dimension to <dim>, default is %lu\n",
268 #if defined(__MSDOS__) || defined(MSDOS)
269 (unsigned long)1000);
270 #else
271 (unsigned long)10000);
272 #endif
273 fprintf(stderr, " -c set number of cycles to <cycles>, default is 10\n");
274 fprintf(stderr, " -h print this help\n");
275 }