Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[6model/c] use OpenMP for threading if available

  • Loading branch information...
commit 24a54a48cb9deca0e52266c5785b73b470fac85d 1 parent 5fa7d52
Martin Berends authored August 10, 2011
45  c/Configure.bat
@@ -7,31 +7,30 @@
7 7
 :: - Add a --help option and provide for more options in general
8 8
 
9 9
 @echo off
10  
-:: Determine whether the C compiler is MSVC or GCC, by attempting to use
11  
-:: them in that order.
  10
+:: Determine whether the C compiler is cl (Microsoft C) or mingw32-gcc,
  11
+:: by attempting to use each one on a very small C program.
12 12
 echo int main(int argc, char * argv[]) { return 0; } >tools\build\temp.c
13 13
 cl -nologo -Fotools\build\temp.obj -Fetools\build\temp.exe tools\build\temp.c >nul 2>nul
14  
-if not errorlevel 1 goto :msvc
15  
-gcc -otools\build\temp.exe tools\build\temp.c
16  
-if not errorlevel 1 goto :gcc
17  
-cc tools\build\temp.c
18  
-if not errorlevel 1 goto :cc
19  
-echo Sorry! Cannot compile with eith cl, gcc or cc.  Please fix.
  14
+if not errorlevel 1 goto cl
  15
+mingw32-gcc -otools\build\temp.exe tools\build\temp.c
  16
+if not errorlevel 1 goto mingw32-gcc
  17
+echo Sorry! Cannot compile with either cl or mingw32-gcc.  Please fix.
20 18
 if exist tools\build\temp.exe del tools\build\temp.exe
21 19
 if exist tools\build\temp.obj del tools\build\temp.obj
22 20
 if exist tools\build\temp.c   del tools\build\temp.c
23 21
 goto :end_of_script
24  
-:msvc
  22
+
  23
+:cl
25 24
 echo Detected Microsoft Visual C/C++ (cl.exe)
26 25
 del tools\build\temp.obj tools\build\temp.exe tools\build\temp.c
27  
-set COMPILER=MSVC
  26
+set COMPILER=cl
28 27
 goto :got_compiler
29  
-:gcc
30  
-echo Detected GNU Compiler Collection (gcc.exe)
  28
+
  29
+:mingw32-gcc
  30
+echo Detected MinGW GCC (mingw32-gcc.exe)
31 31
 del tools\build\temp.exe tools\build\temp.c
32  
-set COMPILER=GCC
  32
+set COMPILER=mingw32-gcc
33 33
 goto :got_compiler
34  
-:cc
35 34
 
36 35
 :got_compiler
37 36
 echo Compiling tools\build\Configure.c to tools\build\Configure.exe
@@ -47,10 +46,10 @@ echo Compiling tools\build\Configure.c to tools\build\Configure.exe
47 46
 :: are not blessed with strcpy_s and so on, this project opts to
48 47
 :: continue using the "older, less secure functions" and disable the
49 48
 :: warnings with -D_CRT_SECURE_NO_WARNINGS
50  
-set opts_msvc=-nologo -Wall -DCC=MSVC -D_CRT_SECURE_NO_WARNINGS -wd4820 -wd4668 -wd4255
51  
-set opts_gcc=-Wall -DCC=GCC
52  
-if "%COMPILER%"=="MSVC" cl %opts_msvc% -Fotools\build\Configure.obj -Fetools\build\Configure.exe tools\build\Configure.c
53  
-if "%COMPILER%"=="GCC" gcc %opts_gcc% -otools\build\Configure.exe tools\build\Configure.c
  49
+set opts_msc=-nologo -Wall -openmp -D_CRT_SECURE_NO_WARNINGS -wd4820 -wd4668 -wd4255
  50
+set opts_gcc=-Wall -fopenmp
  51
+if "%COMPILER%"=="cl" cl %opts_msvc% -Fotools\build\Configure.obj -Fetools\build\Configure.exe tools\build\Configure.c
  52
+if "%COMPILER%"=="mingw32-gcc" mingw32-gcc %opts_gcc% -otools\build\Configure.exe tools\build\Configure.c
54 53
 if errorlevel 1 goto :end_of_script
55 54
 if exist tools\build\Configure.obj del tools\build\Configure.obj
56 55
 :: echo Run tools\build\Configure.exe to create Makefile
@@ -61,9 +60,9 @@ tools\build\Configure.exe tools\build\Makefile.in Makefile
61 60
 :: Notes
62 61
 
63 62
 :: Configure.bat or Configure.cmd?
64  
-:: There is almost no difference between the two file types (just something
65  
-:: subtle about errorlevel), and in 6model/c they work the same.  So on user
66  
-:: friendliness grounds, .bat reassures the reader that the script will do
67  
-:: only "simple" things.
68  
-:: You could argue it the other way - we use cmd.exe, not command.com, so we
  63
+:: Windows XP and later handle the two file types in almost exactly the
  64
+:: same way (there is a minor detail about errorlevel).  In 6model/c
  65
+:: either file type would work the same.  So for user friendliness, .bat
  66
+:: reassures the user that the script does only simple things.  There is
  67
+:: the opposite rationale, that it needs cmd.exe not command.com, and
69 68
 :: should use the extension that command.com cannot handle.  Dunno...
8  c/Configure.sh
... ...
@@ -1,7 +1,5 @@
1 1
 #!/bin/sh
2  
-# TODO
3  
-# - Add a --help option and provide for more options in general
4  
-export COMPILER=GCC
5  
-set opts_gcc=-Wall -DCC=$COMPILER
6  
-cc $opts_gcc -o tools/build/Configure tools/build/Configure.c
  2
+# TODO: Add a --help option and provide for more options in general
  3
+OPTS_GCC='-Wall -fopenmp'
  4
+gcc $OPTS_GCC -o tools/build/Configure tools/build/Configure.c
7 5
 tools/build/Configure tools/build/Makefile.in Makefile
14  c/src/threads.c
@@ -5,6 +5,7 @@
5 5
 /* is a very thin wrapper on Posix systems, and does a lightweight */
6 6
 /* emulation elsewhere. */
7 7
 
  8
+
8 9
 #include "threads.h"  /* thread_create */
9 10
 
10 11
 
@@ -38,9 +39,18 @@ thread_join(struct thread_info * info)
38 39
 }
39 40
 
40 41
 
  42
+/* Caution - do not underestimate the overheads of creating and */
  43
+/* synchronizing threads.  See for example the HPL-2004-209 report */
  44
+/* below, stating that some of the machine code instructions took */
  45
+/* over 100 machine cycles on a Pentium 4.  More detailed and recent */
  46
+/* performance figures would be very welcome. */
  47
+
41 48
 /* See also: */
42 49
 /* Lawrence Livermore National Laboratory tutorials: */
43  
-/* pthreads https://computing.llnl.gov/tutorials/pthreads/ */
44  
-/* OpenMP https://computing.llnl.gov/tutorials/openMP/ */
  50
+/*   pthreads https://computing.llnl.gov/tutorials/pthreads/ */
  51
+/*   OpenMP https://computing.llnl.gov/tutorials/openMP/ */
  52
+/* Threads Cannot be Implemented as a Library (may now be outdated): */
  53
+/*   http://www.hpl.hp.com/techreports/2004/HPL-2004-209.html */
  54
+/* http://en.wikipedia.org/wiki/Lock-free_and_wait-free_algorithms */
45 55
 
46 56
 /* end of threads.c */
18  c/t/01-toolchain/01a-cc.c
@@ -20,7 +20,7 @@
20 20
 #include <stdio.h>   /* fclose fopen fprintf perror printf */
21 21
 #include <stdlib.h>  /* system */
22 22
 
23  
-#ifdef _WIN32
  23
+#if defined( _WIN32 )
24 24
     #include <windows.h>
25 25
     #define pclose _pclose
26 26
     #define popen  _popen
@@ -66,7 +66,7 @@ create_exe()
66 66
     printf("ok 1 - create testexe.c\n");
67 67
 
68 68
     /* test 2: compile the source file to an executable */
69  
-    #ifdef MSVC
  69
+    #if defined( _MSC_VER )
70 70
         status = system("cl -WX -Fetestexe.exe -nologo testexe.c >nul");
71 71
         if (status==0)
72 72
             status = unlink("testexe" EXT_OBJ);
@@ -88,7 +88,7 @@ run_exe()
88 88
     int status, c;
89 89
     FILE * childfile;
90 90
     char inputbuffer[80], * pbuffer, * pbufferend, * testcommand;
91  
-    #ifdef _WIN32
  91
+    #if defined( _WIN32 )
92 92
         testcommand = ".\\testexe foo bar";
93 93
     #else
94 94
         testcommand = "./testexe foo bar";
@@ -103,7 +103,7 @@ run_exe()
103 103
 
104 104
     /* test 4: read the output of the child process into inputbuffer */
105 105
     pbuffer = inputbuffer;  pbufferend = inputbuffer + 80 - 1;
106  
-    while ((c=getc(childfile))!=EOF) {
  106
+    while ((c=getc(childfile)) != EOF) {
107 107
         * pbuffer = c;
108 108
         if (++pbuffer >= pbufferend) {
109 109
             fprintf(stderr,"not ok 4 - buffer overflow\n");
@@ -172,7 +172,7 @@ create_lib()
172 172
         "}\n"
173 173
     );
174 174
     fclose(testlib_sourcefile);
175  
-    #ifdef _WIN32
  175
+    #if defined( _WIN32 )
176 176
         #ifdef _MSC_VER
177 177
             status = system("cl -LD -WX -nologo testlib.c >nul"); /* Visual C++ */
178 178
         #else
@@ -203,7 +203,7 @@ load_lib()
203 203
     char * error;
204 204
     int result;
205 205
     /* test 9: load the library */
206  
-    #ifdef _WIN32
  206
+    #if defined( _WIN32 )
207 207
         FARPROC pfunction;
208 208
         testlib = LoadLibrary("./testlib.dll");
209 209
         if (testlib==NULL) {
@@ -220,7 +220,7 @@ load_lib()
220 220
     #endif
221 221
     printf("ok 9 - load testlib" EXT_DYNLIB "\n");
222 222
 
223  
-    #ifdef _WIN32
  223
+    #if defined( _WIN32 )
224 224
         pfunction = GetProcAddress(testlib, "testfunction");
225 225
     #else
226 226
         dlerror(); /* clear any possible error */
@@ -240,7 +240,7 @@ load_lib()
240 240
         printf("ok 12 - testfunction result\n");
241 241
     else
242 242
         printf("not ok 12 - testfunction result\n");
243  
-    #ifdef _WIN32
  243
+    #if defined( _WIN32 )
244 244
         result = ! FreeLibrary(testlib); /* returns 0 for failure! */
245 245
     #else
246 246
         result = dlclose(testlib);
@@ -264,7 +264,7 @@ remove_lib()
264 264
         exit(14);
265 265
     }
266 266
     printf("ok 14 - remove testlib.c\n");
267  
-    #ifdef _MSC_VER
  267
+    #if defined( _MSC_VER )
268 268
         status = unlink("testlib" EXT_OBJ);
269 269
         if (status) {
270 270
             perror("01a-cc error 15:");
2  c/t/01-toolchain/01b-timing.c
@@ -46,7 +46,7 @@ seconds_microseconds_sleep()
46 46
     /* test 1 - time1 is nonzero */
47 47
     sprintf(message, "clock returned %lld seconds and %d microseconds",
48 48
         seconds1, microseconds1);
49  
-    ok(seconds1 > 0L && microseconds1 > 0, message);
  49
+    ok(seconds1 > 0L, message);
50 50
 
51 51
     /* test 2 - time2 >= time1 */
52 52
     sprintf(message, "clock returned %lld seconds and %d microseconds",
19  c/t/Test.h
@@ -2,25 +2,30 @@
2 2
 
3 3
 /* Lightweight TAP (Test Anything Protocol) emitter in C macros. */
4 4
 
5  
-/* Example usage:
6  
-
7  
-#include <string.h>
  5
+/* SYNOPSIS
8 6
 #include "../Test.h"
9 7
 
10 8
 int
11 9
 main(int argc, char * argv[])
12 10
 {
13  
-    char s[7];
14 11
     plan(1);
15 12
     ok(1==1, "1 is equal to 1");
16  
-    is_ii(1+1, 2, "1 plus 1 is equal to 2");
17  
-    strcpy(s, "foo"); strcat(s, "bar");
18  
-    is_ss(s, "foobar", "strcpy and strcat");
  13
+    is_ii(2 + 2, 4, "2 plus 2 equals 4");
19 14
     return 0;
20 15
 }
  16
+*/
21 17
 
  18
+/* DEFINITIONS
  19
+plan(i);
  20
+diag(s);
  21
+is_ii(i1, i2, "integers match");
  22
+is_ss(s1, s2, "strings match");
  23
+ok(b, "any boolean expression");
  24
+TODO: is_pp(p1, p2, "pointers match");
  25
+TODO: done_testing();
22 26
 */
23 27
 
  28
+
24 29
 #include <stdio.h>  /* printf */
25 30
 
26 31
 int _test_number=0; /* yes, namespace pollution. patches welcome ;-) */
154  c/tools/build/Configure.c
@@ -14,61 +14,33 @@
14 14
 /* Systems usually predefine some variables and macros.  Users might */
15 15
 /* make the same definitions. */
16 16
 
17  
-/*
18  
- * TODO
19  
-
20  
- * Explore more Win32 C compilers and toolchains
21  
- * http://www.thefreecountry.com/compilers/cpp.shtml
22  
- * Most of them alias cc to their own filenames.
23  
-
  17
+/* Currently works on:
24 18
  * MinGW
25 19
  * http://mingw.org/
26 20
  * Currently based on GCC 4.5.2, 85MB disk.
27 21
  * Targets Win32 libraries, no Posix emulation or dlopen.
28 22
  * (older version bundled with Git full install)
29 23
 
30  
- * msysGit
31  
- * The full install is a 39MB download instead of the 13MB Git install, but it expands to 1.3GB instead of
32  
- * about 200MB, and includes MinGW, which includes GCC.
33  
- * It includes bash, so use Configure.sh rather than Configure.bat
34  
-
35  
- * lcc-win32
36  
- * http://www.cs.virginia.edu/~lcc-win32/
37  
-
38 24
  * Microsoft Visual C++ Express Edition 1-2GB RAM, 3GB disk
39 25
  * Registration required to avoid de-activation after 30 days.  Downloads a 3.2MB web installer.
40 26
  * http://www.microsoft.com/express/vc/ do not need optional SQL express.
41 27
  * Also installs Windows Installer 4.5, .NET Framework 4, SQL Server Compact 3.5, Help Viewer 1.0
42 28
  * (download 146MB, disk space 2.3GB)
43  
-
44  
- * Borland
45  
- * Registration required
46  
-
47  
- * Tiny C Compiler
48  
- * http://bellard.org/tcc/
49  
-
50  
- * OpenWatCom
51  
- * http://www.openwatcom.org/index.php/Download
52  
-
53  
- * Digital Mars
54  
- * http://www.digitalmars.com/download/freecompiler.html
55  
-
56  
-*/
  29
+ */
57 30
 
58 31
 #include <stdio.h>   /* fclose fgets FILE fopen fprintf printf stderr */
59 32
 #include <stdlib.h>  /* exit free getenv malloc realloc */
60 33
 #include <string.h>  /* memmove memcpy strcpy strlen strstr */
61  
-#ifdef __APPLE__
  34
+#if defined( __APPLE__ )
62 35
     #include <sys/sysctl.h>
63  
-#else
64  
-    #ifdef __linux__
65  
-        #include <unistd.h>   /* sysconf */
66  
-    #else
67  
-        #ifdef _WIN32
68  
-            #include <windows.h>  /* GetSystemInfo */
69  
-            /* note Microsoft C 2010 needs -wd4820 -wd4668 -wd4255 */
70  
-        #endif
71  
-    #endif
  36
+#elif defined( __linux__ )
  37
+    #include <unistd.h>   /* sysconf */
  38
+#elif defined( _WIN32 )
  39
+    #include <windows.h>  /* GetSystemInfo */
  40
+    /* note Microsoft C 2010 needs -wd4820 -wd4668 -wd4255 */
  41
+#endif
  42
+#if defined( _OPENMP )
  43
+    #include <omp.h> /* on Debian/Ubuntu install gcc-4.4-source */
72 44
 #endif
73 45
 
74 46
 #define LINEBUFFERSIZE 128
@@ -77,9 +49,9 @@ enum { OS_VAR,
77 49
        DETECTED_END /* This one must always be last */};
78 50
 char * detected[DETECTED_END] = {""};
79 51
 /* Subscript names for configuration strings.  Almost like a hash ;) */
80  
-enum { CC, EXE, LDL, MAKE_COMMAND, OSTYPE, OUTFILE, RM_RF, THREADS,
  52
+enum { CC, EXE, LDL, MAKE_COMMAND, OUTFILE, RM_RF, THREADS,
81 53
        CONFIG_END /* this one must always be last */ };
82  
-       /* note the words OS_TYPE and OUT clash with MinGW */
  54
+       /* note the words and OUT clash with MinGW */
83 55
 char * config[CONFIG_END] = {"", "", "", "", "", "", ""};
84 56
 /* forward references to internal functions */
85 57
 void detect(void);
@@ -128,19 +100,28 @@ detection(void)
128 100
     #endif
129 101
     printf("\n");
130 102
 
131  
-    /* Number of processors */
132  
-    /* from http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine */
133  
-    #if defined( __APPLE__ )
134  
-        int mib[4] = {CTL_HW, HW_NCPU, 0, 0};
135  
-        size_t size = sizeof(processors);
136  
-        sysctl(mib, 2, &processors, &size, NULL, 0);
137  
-    #elif defined( __linux__ )
138  
-        processors = sysconf(_SC_NPROCESSORS_ONLN);
139  
-    #elif defined( _WIN32 )
140  
-        GetSystemInfo( &sysinfo );
141  
-        processors = sysinfo.dwNumberOfProcessors;
  103
+    printf("  OpenMP: ");
  104
+    #if defined( _OPENMP )
  105
+        processors = omp_get_num_procs();
  106
+        printf("_OPENMP is %d with max %d threads on %d processors\n",
  107
+            _OPENMP, omp_get_max_threads(), processors);
  108
+    #else
  109
+        printf("not enabled in the C compiler\n");
  110
+
  111
+        /* Number of processors */
  112
+        /* from http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine */
  113
+        #if defined( __APPLE__ )
  114
+            int mib[4] = {CTL_HW, HW_NCPU, 0, 0};
  115
+            size_t size = sizeof(processors);
  116
+            sysctl(mib, 2, &processors, &size, NULL, 0);
  117
+        #elif defined( __linux__ )
  118
+            processors = sysconf(_SC_NPROCESSORS_ONLN);
  119
+        #elif defined( _WIN32 )
  120
+            GetSystemInfo( &sysinfo );
  121
+            processors = sysinfo.dwNumberOfProcessors;
  122
+        #endif
  123
+        printf("  Processor count from operating system: %d\n", processors);
142 124
     #endif
143  
-    printf("  Number of processors: %d\n", processors);
144 125
 
145 126
     /* Sizes of built in data types */
146 127
     printf("Data sizes:  char short int long long_long float double pointer\n");
@@ -160,32 +141,26 @@ detection(void)
160 141
 void
161 142
 config_set(void)
162 143
 {
163  
-    char * s;
164 144
     /* Operating system */
165  
-    s=getenv("OS");
166  
-    if (s && strcmp(s,"Windows_NT")==0) { /* any Windows system */
167  
-	    config[OSTYPE] = "Windows";
  145
+    #if defined( _WIN32 )
168 146
 	    config[EXE] = ".exe";
169  
-    }
170  
-	else {  /* non Windows operating systems default to Unix settings */
171  
-	    config[OSTYPE] = "Unix";
  147
+    #else
172 148
 	    config[EXE] = "";
173  
-    }
174  
-    /* C compiler */ s=getenv("COMPILER");
175  
-    if (s && strcmp(s,"MSVC")==0) {
176  
-        /* TODO: use _MSC_VER */
  149
+    #endif
  150
+
  151
+    /* C compiler */
  152
+    #if defined( _MSC_VER )
177 153
         /* See http://msdn.microsoft.com/en-US/library/b0084kay%28v=VS.100%29.aspx */
178  
-        config[CC] = "cl -DMSVC ";
  154
+        config[CC] = "cl ";
179 155
 	    config[OUTFILE] = "-Fe";
180  
-    }
181  
-    if (s && strcmp(s,"GCC")==0) {
182  
-        s=getenv("OS");
183  
-        if (s && strcmp(s,"Windows_NT")==0)
184  
-            config[CC] = "gcc -DGCC ";
185  
-        else
186  
-            config[CC] = "cc -DGCC -ldl ";
187  
-	    config[OUTFILE] = "-o";
188  
-    }
  156
+    #elif defined( __GNUC__ )
  157
+        #if defined( _WIN32 )
  158
+            config[CC] = "mingw32-gcc ";
  159
+        #else
  160
+            config[CC] = "gcc -ldl ";
  161
+        #endif
  162
+        config[OUTFILE] = "-o";
  163
+    #endif
189 164
 
190 165
     /* File delete command */
191 166
     config[RM_RF] =
@@ -207,12 +182,17 @@ config_set(void)
207 182
         "";
208 183
     #endif
209 184
 
210  
-    /* Make utility */ s=getenv("COMPILER");
211  
-    if (s && strcmp(s,"GCC")==0) {
212  
-	    config[MAKE_COMMAND] = "make";
213  
-    } else {
214  
-	    config[MAKE_COMMAND] = "nmake";
215  
-    }
  185
+    /* Make utility */
  186
+    config[MAKE_COMMAND] =
  187
+    #if defined( _WIN32 )
  188
+        #if defined( __GNUC__ )
  189
+            "mingw32-make";
  190
+        #else
  191
+            "nmake";
  192
+        #endif
  193
+    #else
  194
+	    "make";
  195
+    #endif
216 196
 }
217 197
 
218 198
 
@@ -355,6 +335,20 @@ main(int argc, char * argv[])
355 335
     return 0;
356 336
 }
357 337
 
  338
+/*
  339
+ * TODO
  340
+
  341
+ * Explore more Win32 C compilers and toolchains
  342
+ * http://www.thefreecountry.com/compilers/cpp.shtml
  343
+ * lcc-win32 http://www.cs.virginia.edu/~lcc-win32/
  344
+ * Borland (Registration required)
  345
+ * Tiny C Compiler http://bellard.org/tcc/
  346
+ * OpenWatcom http://www.openwatcom.org/index.php/Download
  347
+ * Digital Mars http://www.digitalmars.com/download/freecompiler.html
  348
+
  349
+*/
  350
+
  351
+
358 352
 /* See also: */
359 353
 /* Autoconf http://www.gnu.org/software/autoconf */
360 354
 /* Automake http://www.gnu.org/software/automake */

0 notes on commit 24a54a4

Please sign in to comment.
Something went wrong with that request. Please try again.