From b9d1ccd40a29a833e5b6589124aa408b2d62386d Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Sun, 10 Apr 2022 11:26:59 +0200 Subject: [PATCH 01/22] Progress towards ARM64 support Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 2 -- src/owl/config/configure.ml | 6 ------ src/owl/core/owl_core_utils.c | 9 ++++++++- src/owl/core/owl_macros.h | 4 +++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 95931ea96..e253a87de 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -34,8 +34,6 @@ let get_default_cflags = | Not_found -> [ "-g" ; "-O3" - ; "-Ofast" - ; "-march=native" ; "-funroll-loops" ; "-ffast-math" ; "-DSFMT_MEXP=19937" diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 77e4af0bc..2650ecb96 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -87,17 +87,11 @@ let default_cflags = [ (* Basic optimisation *) "-g" ; "-O3" - ; "-Ofast" - ; (* FIXME: experimental switches *) - (* "-mavx2"; "-mfma"; "-ffp-contract=fast"; *) (* Experimental switches, -ffast-math may break IEEE754 semantics*) - "-march=native" - ; "-mfpmath=sse" ; "-funroll-loops" ; "-ffast-math" ; (* Configure Mersenne Twister RNG *) "-DSFMT_MEXP=19937" - ; "-msse2" ; "-fno-strict-aliasing" ] diff --git a/src/owl/core/owl_core_utils.c b/src/owl/core/owl_core_utils.c index 1a5a09e9f..ea36bf306 100644 --- a/src/owl/core/owl_core_utils.c +++ b/src/owl/core/owl_core_utils.c @@ -207,7 +207,13 @@ void c_slicing_offset (struct caml_ba_array *X, int64_t *slice, int64_t *offset) * Code heavily inspired by Eigen (http://eigen.tuxfamily.org/). */ - +#if defined(__arm__) || defined(__aarch64__) +void query_cache_sizes(int* l1p, int* l2p, int* l3p) { + *l1p = 16 * 1024; + *l2p = 512 * 1024; + *l3p = 512 * 1024; +} +#elif OWL_INLINE void query_cache_sizes_intel(int* l1p, int* l2p, int* l3p) { int cpuinfo[4]; int l1 = 0, l2 = 0, l3 = 0; @@ -295,3 +301,4 @@ void query_cache_sizes(int* l1p, int* l2p, int* l3p) { *l3p = 512 * 1024; } } +#endif diff --git a/src/owl/core/owl_macros.h b/src/owl/core/owl_macros.h index 7a17e56db..195dcf62b 100644 --- a/src/owl/core/owl_macros.h +++ b/src/owl/core/owl_macros.h @@ -86,6 +86,8 @@ typedef struct { double r, i; } complex_double; #define OWL_ARCH_x86_64 0 #endif +#if defined(__arm__) || defined(__aarch64__) +#elif #if defined(__PIC__) && OWL_ARCH_i386 #define CPUID(cpuinfo,func,id) \ __asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx,%k1": "=a" (cpuinfo[0]), "=&r" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "a" (func), "c" (id)); @@ -96,7 +98,7 @@ typedef struct { double r, i; } complex_double; #define CPUID(cpuinfo,func,id) \ __asm__ __volatile__ ("cpuid": "=a" (cpuinfo[0]), "=b" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "0" (func), "2" (id) ); #endif - +#endif // Other From 8044bb5601b78322488a89036b77fba4d9f62e90 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 11 Apr 2022 14:33:05 +0200 Subject: [PATCH 02/22] Re-introduce Ofast flag Signed-off-by: Marcello Seri --- INSTALL.md | 8 ++++---- src/aeos/config/configure.ml | 1 + src/owl/config/configure.ml | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 614ab1a57..68f5d5b38 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -15,18 +15,18 @@ before compilation: - `OWL_CFLAGS` allows to change the default flags passed to the C targets, it defaults to ``` - OWL_CFLAGS="-g -O3 -Ofast -march=native -mfpmath=sse -funroll-loops -ffast-math -DSFMT_MEXP=19937 -msse2 -fno-strict-aliasing -Wno-tautological-constant-out-of-range-compare"` + OWL_CFLAGS="-g -O3 -Ofast -funroll-loops -ffast-math -DSFMT_MEXP=19937 -msse2 -fno-strict-aliasing"` ``` - `OWL_AEOS_CFLAGS` allows to change the default flags passed to the C targets when compiling AEOS. It defaults to ``` - OWL_AEOS_CFLAGS="-g -O3 -Ofast -march=native -funroll-loops -ffast-math -DSFMT_MEXP=19937 -fno-strict-aliasing" + OWL_AEOS_CFLAGS="-g -O3 -Ofast -funroll-loops -ffast-math -DSFMT_MEXP=19937 -fno-strict-aliasing" ``` - `OWL_DISABLE_LAPACKE_LINKING_FLAG=1` disables the `-llapacke` flag in the - linking options. This is useful when you have lapacke installed on - non-standard location. + linking options. This may be useful when you have certain unusual lapacke + setups, do not use it unless you know what you are doing. If you are not using `opam`, you should run `make clean` before recompiling the library after having changed any of those environment variables. diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index e253a87de..7d9f734a5 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -34,6 +34,7 @@ let get_default_cflags = | Not_found -> [ "-g" ; "-O3" + ; "-Ofast" ; "-funroll-loops" ; "-ffast-math" ; "-DSFMT_MEXP=19937" diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 2650ecb96..f6e3ac3f6 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -87,6 +87,7 @@ let default_cflags = [ (* Basic optimisation *) "-g" ; "-O3" + ; "-Ofast" (* Experimental switches, -ffast-math may break IEEE754 semantics*) ; "-funroll-loops" ; "-ffast-math" From b5db63299b4dee7078008a48bef784c3a3ed4555 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 11 Apr 2022 10:05:51 +0200 Subject: [PATCH 03/22] unit_signal: fix test Signed-off-by: Marcello Seri --- test/unit_signal.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit_signal.ml b/test/unit_signal.ml index 9eea83008..046b12f2d 100644 --- a/test/unit_signal.ml +++ b/test/unit_signal.ml @@ -75,7 +75,7 @@ module To_test = struct M.set freqz_ref [| 3 |] { Complex.re = -0.0536; im = 0.0464 }; let f = freqz ~n:4 ~whole:false (freqz_num |> M.to_array) (freqz_den |> M.to_array) - |> fun (a, b) -> b + |> snd in let max_err = (Owl_dense_ndarray.Z.map2 (fun x a -> Complex.sub x a) f freqz_ref From ac2e0d7221e73a3649d8663cfeaf9144bcd7f0dd Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 11 Apr 2022 17:14:29 +0200 Subject: [PATCH 04/22] Update architecture detection Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 35 ++++++++++++++++++++++-------- src/owl/config/configure.ml | 42 ++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 7d9f734a5..0294b0aed 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -24,7 +24,7 @@ let get_os_type c = | None -> "" -let get_default_cflags = +let get_default_cflags c = try Sys.getenv "OWL_AEOS_CFLAGS" |> String.trim @@ -32,14 +32,31 @@ let get_default_cflags = |> List.filter (fun s -> String.trim s <> "") with | Not_found -> - [ "-g" - ; "-O3" - ; "-Ofast" - ; "-funroll-loops" - ; "-ffast-math" - ; "-DSFMT_MEXP=19937" - ; "-fno-strict-aliasing" - ] + let arch = + let defines = + C.C_define.import + c + ~includes:[] + [ "__x86_64__", Switch + ; "__i386__", Switch + ; "__aarch64__", Switch + ; "__arm__", Switch + ] + in + let open C in + match List.map snd defines with + | Switch true :: _ -> `x86_64 + | _ :: Switch true :: _ -> `x86 + | _ :: _ :: Switch true :: _ -> `arm64 + | _ :: _ :: _ :: Switch true :: _ -> `arm + | _ -> `unknown + in + [ "-g"; "-O3"; "-Ofast" ] + @ (match arch with + | `x86_64 -> [ "-march=native"; "-msse2" ] + | `arm64 -> [ "-mcpu=apple-m1" ] + | _ -> []) + @ [ "-funroll-loops"; "-ffast-math"; "-DSFMT_MEXP=19937"; "-fno-strict-aliasing" ] let get_openmp_cflags c = diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index f6e3ac3f6..a93d7b4d3 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -84,17 +84,37 @@ let default_cflags = |> List.filter (fun s -> String.trim s <> "") with | Not_found -> - [ (* Basic optimisation *) - "-g" - ; "-O3" - ; "-Ofast" - (* Experimental switches, -ffast-math may break IEEE754 semantics*) - ; "-funroll-loops" - ; "-ffast-math" - ; (* Configure Mersenne Twister RNG *) - "-DSFMT_MEXP=19937" - ; "-fno-strict-aliasing" - ] + let arch = + let defines = + C.C_define.import + c + ~includes:[] + [ "__x86_64__", Switch + ; "__i386__", Switch + ; "__aarch64__", Switch + ; "__arm__", Switch + ] + in + let open C in + match List.map snd defines with + | Switch true :: _ -> `x86_64 + | _ :: Switch true :: _ -> `x86 + | _ :: _ :: Switch true :: _ -> `arm64 + | _ :: _ :: _ :: Switch true :: _ -> `arm + | _ -> `unknown + in + [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] + @ (match arch with + | `x86_64 -> [ "-march=native"; "-msse2" ] + | `arm64 -> [ "-mcpu=apple-m1" ] + | _ -> []) + @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) + "-funroll-loops" + ; "-ffast-math" + ; (* Configure Mersenne Twister RNG *) + "-DSFMT_MEXP=19937" + ; "-fno-strict-aliasing" + ] let default_libs = [ "-lm" ] From 7a3c434a926fea40f930aab906eb658f18d9c9ca Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 11 Apr 2022 17:14:40 +0200 Subject: [PATCH 05/22] ocamlformat Signed-off-by: Marcello Seri --- test/unit_signal.ml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit_signal.ml b/test/unit_signal.ml index 046b12f2d..bf5bec030 100644 --- a/test/unit_signal.ml +++ b/test/unit_signal.ml @@ -74,8 +74,7 @@ module To_test = struct M.set freqz_ref [| 2 |] { Complex.re = -0.5; im = -0.5 }; M.set freqz_ref [| 3 |] { Complex.re = -0.0536; im = 0.0464 }; let f = - freqz ~n:4 ~whole:false (freqz_num |> M.to_array) (freqz_den |> M.to_array) - |> snd + freqz ~n:4 ~whole:false (freqz_num |> M.to_array) (freqz_den |> M.to_array) |> snd in let max_err = (Owl_dense_ndarray.Z.map2 (fun x a -> Complex.sub x a) f freqz_ref From efc51a15450eca2ef6a712f994cafab6d193d930 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 11 Apr 2022 17:17:59 +0200 Subject: [PATCH 06/22] fixup! Update architecture detection --- src/aeos/config/configure.ml | 2 +- src/owl/config/configure.ml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 0294b0aed..160c3aa11 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -92,7 +92,7 @@ let () = (* configure link options *) let libs = [] @ get_default_libs () @ get_openmp_libs c in (* configure compile options *) - let cflags = [] @ get_default_cflags @ get_openmp_cflags c in + let cflags = [] @ get_default_cflags c @ get_openmp_cflags c in (* assemble default config *) let conf : C.Pkg_config.package_conf = { cflags; libs } in C.Flags.write_sexp "aeos_c_flags.sexp" conf.cflags; diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index a93d7b4d3..3a79a16c0 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -76,7 +76,7 @@ let get_ocaml_devmode_flags _c = if not enable_devmode then [] else [ "-w"; "-32-27-6-37-3" ] -let default_cflags = +let default_cflags c = try Sys.getenv "OWL_CFLAGS" |> String.trim @@ -268,7 +268,7 @@ some details on how your openblas has been installed and the output of [] @ openblas_conf.cflags @ cblas_conf.cflags - @ default_cflags + @ default_cflags c @ get_devmode_cflags c @ get_expmode_cflags c @ openmp_config.cflags From 48a8b0e257c87be43f7f2c727f26a1087e19c5e1 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Tue, 12 Apr 2022 18:15:54 +0200 Subject: [PATCH 07/22] Improve architecture and os detection Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 57 ++++++++++++++++++++++++++++++------ src/owl/config/configure.ml | 49 ++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 13 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 160c3aa11..4b15c973e 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -5,6 +5,27 @@ module C = Configurator.V1 +let detect_system_header = + {| + #if __APPLE__ + #include + #if TARGET_OS_IPHONE + #define PLATFORM_NAME "ios" + #else + #define PLATFORM_NAME "mac" + #endif + #elif __linux__ + #if __ANDROID__ + #define PLATFORM_NAME "android" + #else + #define PLATFORM_NAME "linux" + #endif + #elif WIN32 + #define PLATFORM_NAME "windows" + #endif +|} + + let bgetenv v = let v' = try Sys.getenv v |> int_of_string with @@ -32,6 +53,25 @@ let get_default_cflags c = |> List.filter (fun s -> String.trim s <> "") with | Not_found -> + let os = + let header = + let file = Filename.temp_file "discover" "os.h" in + let fd = open_out file in + output_string fd detect_system_header; + close_out fd; + file + in + let platform = + C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] + in + match List.map snd platform with + | [ String "android" ] -> `android + | [ String "ios" ] -> `ios + | [ String "linux" ] -> `linux + | [ String "mac" ] -> `mac + | [ String "windows" ] -> `windows + | _ -> `unknown + in let arch = let defines = C.C_define.import @@ -52,10 +92,11 @@ let get_default_cflags c = | _ -> `unknown in [ "-g"; "-O3"; "-Ofast" ] - @ (match arch with - | `x86_64 -> [ "-march=native"; "-msse2" ] - | `arm64 -> [ "-mcpu=apple-m1" ] - | _ -> []) + @ (match arch, os with + | `arm64, `mac -> [ "-mcpu=apple-m1" ] + | `arm64, _ -> [ "-march=native" ] + | `x86_64, _ -> [ "-march=native"; "-msse2" ] + | _ -> []) @ [ "-funroll-loops"; "-ffast-math"; "-DSFMT_MEXP=19937"; "-fno-strict-aliasing" ] @@ -65,11 +106,9 @@ let get_openmp_cflags c = then [] else ( match get_os_type c with - | "linux" -> [ "-fopenmp" ] - | "linux_elf" -> [ "-fopenmp" ] - | "macosx" -> [ "-Xpreprocessor"; "-fopenmp" ] - | "mingw64" -> [ "-fopenmp" ] - | _ -> []) + | "mingw64" | "linux" | "linux_elf" -> [ "-fopenmp" ] + | "macosx" -> [ "-Xpreprocessor"; "-fopenmp" ] + | _ -> []) let get_default_libs () = [ "-lm" ] diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 3a79a16c0..678dd668e 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -5,6 +5,27 @@ module C = Configurator.V1 +let detect_system_header = + {| + #if __APPLE__ + #include + #if TARGET_OS_IPHONE + #define PLATFORM_NAME "ios" + #else + #define PLATFORM_NAME "mac" + #endif + #elif __linux__ + #if __ANDROID__ + #define PLATFORM_NAME "android" + #else + #define PLATFORM_NAME "linux" + #endif + #elif WIN32 + #define PLATFORM_NAME "windows" + #endif +|} + + let bgetenv v = let v' = try Sys.getenv v |> int_of_string with @@ -84,6 +105,25 @@ let default_cflags c = |> List.filter (fun s -> String.trim s <> "") with | Not_found -> + let os = + let header = + let file = Filename.temp_file "discover" "os.h" in + let fd = open_out file in + output_string fd detect_system_header; + close_out fd; + file + in + let platform = + C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] + in + match List.map snd platform with + | [ String "android" ] -> `android + | [ String "ios" ] -> `ios + | [ String "linux" ] -> `linux + | [ String "mac" ] -> `mac + | [ String "windows" ] -> `windows + | _ -> `unknown + in let arch = let defines = C.C_define.import @@ -104,10 +144,11 @@ let default_cflags c = | _ -> `unknown in [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] - @ (match arch with - | `x86_64 -> [ "-march=native"; "-msse2" ] - | `arm64 -> [ "-mcpu=apple-m1" ] - | _ -> []) + @ (match arch, os with + | `arm64, `mac -> [ "-mcpu=apple-m1" ] + | `arm64, _ -> [ "-march=native" ] + | `x86_64, _ -> [ "-march=native"; "-msse2" ] + | _ -> []) @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) "-funroll-loops" ; "-ffast-math" From ab17a401abf90560df4e5ee9fb840e4125f8e1ae Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Tue, 12 Apr 2022 18:19:03 +0200 Subject: [PATCH 08/22] config: cleanup unused open Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 1 - src/owl/config/configure.ml | 1 - 2 files changed, 2 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 4b15c973e..2d5bf398c 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -83,7 +83,6 @@ let get_default_cflags c = ; "__arm__", Switch ] in - let open C in match List.map snd defines with | Switch true :: _ -> `x86_64 | _ :: Switch true :: _ -> `x86 diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 678dd668e..775a39988 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -135,7 +135,6 @@ let default_cflags c = ; "__arm__", Switch ] in - let open C in match List.map snd defines with | Switch true :: _ -> `x86_64 | _ :: Switch true :: _ -> `x86 From 7f4cc770f8dda14912d044fc1fee8e4439c3a9e4 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Tue, 12 Apr 2022 18:23:31 +0200 Subject: [PATCH 09/22] reintroduce -mfpmath=sse on x86_64 Signed-off-by: Marcello Seri --- src/owl/config/configure.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 775a39988..5a672e8d0 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -146,7 +146,7 @@ let default_cflags c = @ (match arch, os with | `arm64, `mac -> [ "-mcpu=apple-m1" ] | `arm64, _ -> [ "-march=native" ] - | `x86_64, _ -> [ "-march=native"; "-msse2" ] + | `x86_64, _ -> [ "-march=native"; "-mfpmath=sse"; "-msse2" ] | _ -> []) @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) "-funroll-loops" From 69c7e386af7d22cb9e83033cac315e62c4c27d88 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Thu, 14 Apr 2022 15:32:37 +0200 Subject: [PATCH 10/22] owl_macros.h: remove empty if --- src/owl/core/owl_macros.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/owl/core/owl_macros.h b/src/owl/core/owl_macros.h index 195dcf62b..e3b6f2325 100644 --- a/src/owl/core/owl_macros.h +++ b/src/owl/core/owl_macros.h @@ -86,18 +86,17 @@ typedef struct { double r, i; } complex_double; #define OWL_ARCH_x86_64 0 #endif -#if defined(__arm__) || defined(__aarch64__) -#elif -#if defined(__PIC__) && OWL_ARCH_i386 - #define CPUID(cpuinfo,func,id) \ - __asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx,%k1": "=a" (cpuinfo[0]), "=&r" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "a" (func), "c" (id)); -#elif defined(__PIC__) && OWL_ARCH_x86_64 - #define CPUID(cpuinfo,func,id) \ - __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (cpuinfo[0]), "=&r" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "0" (func), "2" (id)); -#else - #define CPUID(cpuinfo,func,id) \ - __asm__ __volatile__ ("cpuid": "=a" (cpuinfo[0]), "=b" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "0" (func), "2" (id) ); -#endif +#if !defined(__arm__) && !defined(__aarch64__) + #if defined(__PIC__) && OWL_ARCH_i386 + #define CPUID(cpuinfo,func,id) \ + __asm__ __volatile__ ("xchgl %%ebx, %k1; cpuid; xchgl %%ebx,%k1": "=a" (cpuinfo[0]), "=&r" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "a" (func), "c" (id)); + #elif defined(__PIC__) && OWL_ARCH_x86_64 + #define CPUID(cpuinfo,func,id) \ + __asm__ __volatile__ ("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1": "=a" (cpuinfo[0]), "=&r" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "0" (func), "2" (id)); + #else + #define CPUID(cpuinfo,func,id) \ + __asm__ __volatile__ ("cpuid": "=a" (cpuinfo[0]), "=b" (cpuinfo[1]), "=c" (cpuinfo[2]), "=d" (cpuinfo[3]) : "0" (func), "2" (id) ); + #endif #endif // Other From 0b64902f23fad8349a07b2cd2e6cf17b64c67c77 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Thu, 14 Apr 2022 22:47:18 +0200 Subject: [PATCH 11/22] owl/configure: remove devmode cflags They break the compilation with some c compilers. See https://github.com/owlbarn/owl/pull/609 Signed-off-by: Marcello Seri --- src/owl/config/configure.ml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 5a672e8d0..ce123a8ef 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -164,13 +164,6 @@ let get_expmode_cflags _c = if not enable_expmode then [] else [ "-flto" ] -let get_devmode_cflags _c = - let enable_devmode = bgetenv "OWL_ENABLE_DEVMODE" in - if not enable_devmode - then [ "-Wno-logical-op-parentheses" ] - else [ "-Wall"; "-pedantic"; "-Wextra"; "-Wunused" ] - - let default_gcc_path = let p0 = "/usr/local/lib/gcc/7" in if Sys.file_exists p0 then [ "-L" ^ p0 ] else [] @@ -305,11 +298,10 @@ some details on how your openblas has been installed and the output of in (* configure compile options *) let cflags = - [] + [ "-Wall"; "-pedantic"; "-Wextra"; "-Wunused" ] @ openblas_conf.cflags @ cblas_conf.cflags @ default_cflags c - @ get_devmode_cflags c @ get_expmode_cflags c @ openmp_config.cflags in From 729cf0037e22caf2cca6b25a1ffdec20ae0e3112 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Thu, 28 Apr 2022 22:10:30 +0200 Subject: [PATCH 12/22] owl_core_utils: Fix empty elif Signed-off-by: Marcello Seri --- src/owl/core/owl_core_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/owl/core/owl_core_utils.c b/src/owl/core/owl_core_utils.c index ea36bf306..6b2f5b67a 100644 --- a/src/owl/core/owl_core_utils.c +++ b/src/owl/core/owl_core_utils.c @@ -213,7 +213,7 @@ void query_cache_sizes(int* l1p, int* l2p, int* l3p) { *l2p = 512 * 1024; *l3p = 512 * 1024; } -#elif +#else OWL_INLINE void query_cache_sizes_intel(int* l1p, int* l2p, int* l3p) { int cpuinfo[4]; int l1 = 0, l2 = 0, l3 = 0; From 2537f1b6701cc27880c48398953ef4419efe5448 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Fri, 29 Apr 2022 08:24:57 +0200 Subject: [PATCH 13/22] Cleanup arch detection code Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 42 ++++++++++++++++++++++++------------ src/owl/config/configure.ml | 42 ++++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 2d5bf398c..e3218a71b 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -25,6 +25,20 @@ let detect_system_header = #endif |} +let detect_system_arch = + {| + #if __x86_64__ + #define PLATFORM_ARCH "x86_64" + #elif __i386__ + #define PLATFORM_ARCH "x86" + #elif __aarch64__ + #define PLATFORM_ARCH "arm64" + #elif __arm__ + #define PLATFORM_ARCH "arm" + #else + #define PLATFORM_ARCH "unknown" + #endif +|} let bgetenv v = let v' = @@ -73,21 +87,21 @@ let get_default_cflags c = | _ -> `unknown in let arch = - let defines = - C.C_define.import - c - ~includes:[] - [ "__x86_64__", Switch - ; "__i386__", Switch - ; "__aarch64__", Switch - ; "__arm__", Switch - ] + let header = + let file = Filename.temp_file "discover" "arch.h" in + let fd = open_out file in + output_string fd detect_system_arch; + close_out fd; + file + in + let arch = + C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in - match List.map snd defines with - | Switch true :: _ -> `x86_64 - | _ :: Switch true :: _ -> `x86 - | _ :: _ :: Switch true :: _ -> `arm64 - | _ :: _ :: _ :: Switch true :: _ -> `arm + match List.map snd arch with + | [ String "x86_64" ] -> `x86_64 + | [ String "x86" ] -> `x86 + | [ String "arm64" ] -> `arm64 + | [ String "arm" ] -> `arm | _ -> `unknown in [ "-g"; "-O3"; "-Ofast" ] diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index ce123a8ef..cb13dda40 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -25,6 +25,20 @@ let detect_system_header = #endif |} +let detect_system_arch = + {| + #if __x86_64__ + #define PLATFORM_ARCH "x86_64" + #elif __i386__ + #define PLATFORM_ARCH "x86" + #elif __aarch64__ + #define PLATFORM_ARCH "arm64" + #elif __arm__ + #define PLATFORM_ARCH "arm" + #else + #define PLATFORM_ARCH "unknown" + #endif +|} let bgetenv v = let v' = @@ -125,21 +139,21 @@ let default_cflags c = | _ -> `unknown in let arch = - let defines = - C.C_define.import - c - ~includes:[] - [ "__x86_64__", Switch - ; "__i386__", Switch - ; "__aarch64__", Switch - ; "__arm__", Switch - ] + let header = + let file = Filename.temp_file "discover" "arch.h" in + let fd = open_out file in + output_string fd detect_system_arch; + close_out fd; + file + in + let arch = + C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in - match List.map snd defines with - | Switch true :: _ -> `x86_64 - | _ :: Switch true :: _ -> `x86 - | _ :: _ :: Switch true :: _ -> `arm64 - | _ :: _ :: _ :: Switch true :: _ -> `arm + match List.map snd arch with + | [ String "x86_64" ] -> `x86_64 + | [ String "x86" ] -> `x86 + | [ String "arm64" ] -> `arm64 + | [ String "arm" ] -> `arm | _ -> `unknown in [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] From c9a67a415b10c4198c7863f2d3e280852cbbfe82 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Wed, 11 May 2022 14:52:35 +0200 Subject: [PATCH 14/22] configure: update to support new lapacke packages Debian now ships lapacke separately, requiring new set of flags for the linking to work. This uses pkg-config to improve the detection of these flags. Furthermore this commit adds a workaround to solve the issue with libgcc11_s not found, which will not be addressed upstream in homebrew for the time being. Signed-off-by: Marcello Seri --- src/owl/config/configure.ml | 141 ++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 64 deletions(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index cb13dda40..dceba1de2 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -111,64 +111,68 @@ let get_ocaml_devmode_flags _c = if not enable_devmode then [] else [ "-w"; "-32-27-6-37-3" ] -let default_cflags c = - try - Sys.getenv "OWL_CFLAGS" - |> String.trim - |> String.split_on_char ' ' - |> List.filter (fun s -> String.trim s <> "") - with - | Not_found -> - let os = - let header = - let file = Filename.temp_file "discover" "os.h" in - let fd = open_out file in - output_string fd detect_system_header; - close_out fd; - file - in - let platform = - C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] - in - match List.map snd platform with - | [ String "android" ] -> `android - | [ String "ios" ] -> `ios - | [ String "linux" ] -> `linux - | [ String "mac" ] -> `mac - | [ String "windows" ] -> `windows - | _ -> `unknown +let default_config c = + let os = + let header = + let file = Filename.temp_file "discover" "os.h" in + let fd = open_out file in + output_string fd detect_system_header; + close_out fd; + file in - let arch = - let header = - let file = Filename.temp_file "discover" "arch.h" in - let fd = open_out file in - output_string fd detect_system_arch; - close_out fd; - file - in - let arch = - C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] - in - match List.map snd arch with - | [ String "x86_64" ] -> `x86_64 - | [ String "x86" ] -> `x86 - | [ String "arm64" ] -> `arm64 - | [ String "arm" ] -> `arm - | _ -> `unknown + let platform = C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] in + match List.map snd platform with + | [ String "android" ] -> `android + | [ String "ios" ] -> `ios + | [ String "linux" ] -> `linux + | [ String "mac" ] -> `mac + | [ String "windows" ] -> `windows + | _ -> `unknown + in + let arch = + let header = + let file = Filename.temp_file "discover" "arch.h" in + let fd = open_out file in + output_string fd detect_system_arch; + close_out fd; + file in - [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] - @ (match arch, os with - | `arm64, `mac -> [ "-mcpu=apple-m1" ] - | `arm64, _ -> [ "-march=native" ] - | `x86_64, _ -> [ "-march=native"; "-mfpmath=sse"; "-msse2" ] - | _ -> []) - @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) - "-funroll-loops" - ; "-ffast-math" - ; (* Configure Mersenne Twister RNG *) - "-DSFMT_MEXP=19937" - ; "-fno-strict-aliasing" - ] + let arch = C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in + match List.map snd arch with + | [ String "x86_64" ] -> `x86_64 + | [ String "x86" ] -> `x86 + | [ String "arm64" ] -> `arm64 + | [ String "arm" ] -> `arm + | _ -> `unknown + in + let cflags = + try + Sys.getenv "OWL_CFLAGS" + |> String.trim + |> String.split_on_char ' ' + |> List.filter (fun s -> String.trim s <> "") + with + | Not_found -> + [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] + @ (match arch, os with + | `arm64, `mac -> [ "-mcpu=apple-m1" ] + | `arm64, _ -> [ "-march=native" ] + | `x86_64, _ -> [ "-march=native"; "-mfpmath=sse"; "-msse2" ] + | _ -> []) + @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) + "-funroll-loops" + ; "-ffast-math" + ; (* Configure Mersenne Twister RNG *) + "-DSFMT_MEXP=19937" + ; "-fno-strict-aliasing" + ] + in + (* homebrew M1 issue workaround, works only if users use the default homebrew path *) + let libs = + let p0 = "/opt/homebrew/opt/gcc/lib/gcc/11/" in + if os = `mac && arch = `arm64 && Sys.file_exists p0 then [ "-L" ^ p0 ] else [] + in + C.Pkg_config.{ cflags; libs } let default_libs = [ "-lm" ] @@ -246,11 +250,12 @@ the output of `src/owl/config/configure.exe --verbose`. let () = C.main ~name:"owl" (fun c -> + let default_config = default_config c in + let default_pkg_config = { C.Pkg_config.cflags = []; libs = [] } in let cblas_conf = - let default = { C.Pkg_config.cflags = []; libs = [] } in let open Base.Option.Monad_infix in Base.Option.value - ~default + ~default:default_pkg_config (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"cblas") in let openblas_conf = @@ -264,7 +269,7 @@ let () = c test_linking ~c_flags:openblas_conf.cflags - ~link_flags:openblas_conf.libs + ~link_flags:(default_config.libs @ openblas_conf.libs) then ( Printf.printf {| @@ -283,7 +288,7 @@ some details on how your openblas has been installed and the output of Base.(Sexp.to_string @@ sexp_of_list sexp_of_string openblas_conf.cflags) Base.(Sexp.to_string @@ sexp_of_list sexp_of_string openblas_conf.libs); failwith "Unable to link against openblas."); - let lapacke_lib = + let lapacke_conf = let disable_linking_flag = bgetenv "OWL_DISABLE_LAPACKE_LINKING_FLAG" in let needs_lapacke_flag = if disable_linking_flag @@ -293,16 +298,23 @@ some details on how your openblas has been installed and the output of c test_lapacke_working_code ~c_flags:openblas_conf.cflags - ~link_flags:(openblas_conf.libs @ [ "-lm" ]) + ~link_flags:(default_config.libs @ openblas_conf.libs @ [ "-lm" ]) |> not in - if needs_lapacke_flag then [ "-llapacke" ] else [] + if needs_lapacke_flag + then + let open Base.Option.Monad_infix in + Base.Option.value + ~default:C.Pkg_config.{ cflags = []; libs = [ "-llapacke" ] } + (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"llapacke") + else default_pkg_config in let openmp_config = get_openmp_config c in (* configure link options *) let libs = [] - @ lapacke_lib + @ default_config.libs + @ lapacke_conf.libs @ openblas_conf.libs @ cblas_conf.libs @ default_libs @@ -313,9 +325,10 @@ some details on how your openblas has been installed and the output of (* configure compile options *) let cflags = [ "-Wall"; "-pedantic"; "-Wextra"; "-Wunused" ] + @ lapacke_conf.cflags @ openblas_conf.cflags @ cblas_conf.cflags - @ default_cflags c + @ default_config.cflags @ get_expmode_cflags c @ openmp_config.cflags in From 7924c344947048c45065e9753af31ac649b997aa Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Wed, 11 May 2022 14:58:50 +0200 Subject: [PATCH 15/22] ocamlformat the code Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 14 +++++++------- src/owl/config/configure.ml | 10 ++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index e3218a71b..1d467b136 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -25,6 +25,7 @@ let detect_system_header = #endif |} + let detect_system_arch = {| #if __x86_64__ @@ -40,6 +41,7 @@ let detect_system_arch = #endif |} + let bgetenv v = let v' = try Sys.getenv v |> int_of_string with @@ -94,15 +96,13 @@ let get_default_cflags c = close_out fd; file in - let arch = - C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] - in + let arch = C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in match List.map snd arch with | [ String "x86_64" ] -> `x86_64 - | [ String "x86" ] -> `x86 - | [ String "arm64" ] -> `arm64 - | [ String "arm" ] -> `arm - | _ -> `unknown + | [ String "x86" ] -> `x86 + | [ String "arm64" ] -> `arm64 + | [ String "arm" ] -> `arm + | _ -> `unknown in [ "-g"; "-O3"; "-Ofast" ] @ (match arch, os with diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index dceba1de2..2475d9f85 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -25,6 +25,7 @@ let detect_system_header = #endif |} + let detect_system_arch = {| #if __x86_64__ @@ -40,6 +41,7 @@ let detect_system_arch = #endif |} + let bgetenv v = let v' = try Sys.getenv v |> int_of_string with @@ -140,10 +142,10 @@ let default_config c = let arch = C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in match List.map snd arch with | [ String "x86_64" ] -> `x86_64 - | [ String "x86" ] -> `x86 - | [ String "arm64" ] -> `arm64 - | [ String "arm" ] -> `arm - | _ -> `unknown + | [ String "x86" ] -> `x86 + | [ String "arm64" ] -> `arm64 + | [ String "arm" ] -> `arm + | _ -> `unknown in let cflags = try From f9b875aa892a0b5e87cfac85437399355add23eb Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Sat, 14 May 2022 10:52:37 +0200 Subject: [PATCH 16/22] Workaround configurator problem with windows And remove march=native from arm, since it breaks on a number of arm systems Signed-off-by: Marcello Seri --- src/aeos/config/configure.ml | 21 ++++----------------- src/owl/config/configure.ml | 22 +++------------------- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 1d467b136..36a62d455 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -5,7 +5,7 @@ module C = Configurator.V1 -let detect_system_header = +let detect_system_os = {| #if __APPLE__ #include @@ -70,15 +70,8 @@ let get_default_cflags c = with | Not_found -> let os = - let header = - let file = Filename.temp_file "discover" "os.h" in - let fd = open_out file in - output_string fd detect_system_header; - close_out fd; - file - in let platform = - C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] + C.C_define.import c ~includes:[ ] ~prelude:detect_system_os [ "PLATFORM_NAME", String ] in match List.map snd platform with | [ String "android" ] -> `android @@ -89,14 +82,9 @@ let get_default_cflags c = | _ -> `unknown in let arch = - let header = - let file = Filename.temp_file "discover" "arch.h" in - let fd = open_out file in - output_string fd detect_system_arch; - close_out fd; - file + let arch = + C.C_define.import c ~includes:[ ] ~prelude:detect_system_arch [ "PLATFORM_ARCH", String ] in - let arch = C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in match List.map snd arch with | [ String "x86_64" ] -> `x86_64 | [ String "x86" ] -> `x86 @@ -107,7 +95,6 @@ let get_default_cflags c = [ "-g"; "-O3"; "-Ofast" ] @ (match arch, os with | `arm64, `mac -> [ "-mcpu=apple-m1" ] - | `arm64, _ -> [ "-march=native" ] | `x86_64, _ -> [ "-march=native"; "-msse2" ] | _ -> []) @ [ "-funroll-loops"; "-ffast-math"; "-DSFMT_MEXP=19937"; "-fno-strict-aliasing" ] diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 2475d9f85..9726c0bf5 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -5,7 +5,7 @@ module C = Configurator.V1 -let detect_system_header = +let detect_system_os = {| #if __APPLE__ #include @@ -115,14 +115,7 @@ let get_ocaml_devmode_flags _c = let default_config c = let os = - let header = - let file = Filename.temp_file "discover" "os.h" in - let fd = open_out file in - output_string fd detect_system_header; - close_out fd; - file - in - let platform = C.C_define.import c ~includes:[ header ] [ "PLATFORM_NAME", String ] in + let platform = C.C_define.import c ~includes:[ ] ~prelude:detect_system_os [ "PLATFORM_NAME", String ] in match List.map snd platform with | [ String "android" ] -> `android | [ String "ios" ] -> `ios @@ -132,14 +125,7 @@ let default_config c = | _ -> `unknown in let arch = - let header = - let file = Filename.temp_file "discover" "arch.h" in - let fd = open_out file in - output_string fd detect_system_arch; - close_out fd; - file - in - let arch = C.C_define.import c ~includes:[ header ] [ "PLATFORM_ARCH", String ] in + let arch = C.C_define.import c ~includes:[ ] ~prelude:detect_system_arch [ "PLATFORM_ARCH", String ] in match List.map snd arch with | [ String "x86_64" ] -> `x86_64 | [ String "x86" ] -> `x86 @@ -158,7 +144,6 @@ let default_config c = [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] @ (match arch, os with | `arm64, `mac -> [ "-mcpu=apple-m1" ] - | `arm64, _ -> [ "-march=native" ] | `x86_64, _ -> [ "-march=native"; "-mfpmath=sse"; "-msse2" ] | _ -> []) @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*) @@ -176,7 +161,6 @@ let default_config c = in C.Pkg_config.{ cflags; libs } - let default_libs = [ "-lm" ] let get_expmode_cflags _c = From c575988392993a128e934c7c0c2cb2f73570b5e7 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Thu, 2 Jun 2022 22:23:07 +0200 Subject: [PATCH 17/22] configurator: make more robust wrt not officially supported platforms (e.g. bsds) --- src/aeos/config/configure.ml | 2 ++ src/owl/config/configure.ml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index 36a62d455..0fdd25f9e 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -22,6 +22,8 @@ let detect_system_os = #endif #elif WIN32 #define PLATFORM_NAME "windows" + #else + #define PLATFORM_NAME "unknown" #endif |} diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 9726c0bf5..fc59efd15 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -22,6 +22,8 @@ let detect_system_os = #endif #elif WIN32 #define PLATFORM_NAME "windows" + #else + #define PLATFORM_NAME "unknown" #endif |} From 954f498432198cea2f61612350907992c55747dc Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Sat, 8 Oct 2022 16:05:44 +0200 Subject: [PATCH 18/22] Update src/owl/config/configure.ml Co-authored-by: Thomas Gazagnaire --- src/owl/config/configure.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index fc59efd15..22a82a883 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -158,7 +158,7 @@ let default_config c = in (* homebrew M1 issue workaround, works only if users use the default homebrew path *) let libs = - let p0 = "/opt/homebrew/opt/gcc/lib/gcc/11/" in + let p0 = "/opt/homebrew/opt/gcc/lib/gcc/current/" in if os = `mac && arch = `arm64 && Sys.file_exists p0 then [ "-L" ^ p0 ] else [] in C.Pkg_config.{ cflags; libs } From 00e9d3331ee20e587b7d8e9883d0c5f27944fec7 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Mon, 9 Jan 2023 10:26:08 +0100 Subject: [PATCH 19/22] configurator: drop unnecessary dependency on stdio Signed-off-by: Marcello Seri --- owl.opam | 1 - src/aeos/config/dune | 2 +- src/owl/config/configure.ml | 23 ++++++++++------------- src/owl/config/dune | 2 +- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/owl.opam b/owl.opam index 2a44f3871..de6ba7652 100644 --- a/owl.opam +++ b/owl.opam @@ -32,6 +32,5 @@ depends: [ "dune-configurator" "eigen" {>= "0.3.0"} "owl-base" {= version} - "stdio" {build} "npy" ] diff --git a/src/aeos/config/dune b/src/aeos/config/dune index a9a6574c7..cb623703a 100644 --- a/src/aeos/config/dune +++ b/src/aeos/config/dune @@ -1,3 +1,3 @@ (executable (name configure) - (libraries dune.configurator stdio)) + (libraries dune.configurator)) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 22a82a883..a4151c2a2 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -220,7 +220,7 @@ let get_openmp_config c = {| You have set OWL_ENABLE_OPENMP = 1 however I am unable to link against openmp: the current values for cflags and libs are respectively -%s and %s. +(%s) and (%s). You can disable openmp/aeos by unsetting OWL_ENABLE_OPEN or by setting it to 0. @@ -230,25 +230,23 @@ or `dune clean` before rebuilding the project with a modified flag. If you think this is our mistake please open an issue reporting the output of `src/owl/config/configure.exe --verbose`. |} - Base.(string_of_sexp @@ sexp_of_list sexp_of_string cflags) - Base.(string_of_sexp @@ sexp_of_list sexp_of_string libs); + (String.concat " " cflags) + (String.concat " " libs); failwith "Unable to link against openmp"); C.Pkg_config.{ cflags; libs }) let () = C.main ~name:"owl" (fun c -> + let (>>=) = Option.bind in let default_config = default_config c in let default_pkg_config = { C.Pkg_config.cflags = []; libs = [] } in let cblas_conf = - let open Base.Option.Monad_infix in - Base.Option.value - ~default:default_pkg_config + Option.value ~default:default_pkg_config (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"cblas") in let openblas_conf = - let open Base.Option.Monad_infix in - Base.Option.value + Option.value ~default:openblas_default (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"openblas") in @@ -262,7 +260,7 @@ let () = Printf.printf {| Unable to link against openblas: the current values for cflags and libs -are respectively %s and %s. +are respectively (%s) and (%s). Usually this is due to missing paths for pkg-config. Try to re-install or re-compile owl by prefixing the command with (or exporting) @@ -273,8 +271,8 @@ If this does not work please open an issue in the owl repository, adding some details on how your openblas has been installed and the output of `src/owl/config/configure.exe --verbose`. |} - Base.(Sexp.to_string @@ sexp_of_list sexp_of_string openblas_conf.cflags) - Base.(Sexp.to_string @@ sexp_of_list sexp_of_string openblas_conf.libs); + (String.concat " " openblas_conf.cflags) + (String.concat " " openblas_conf.libs); failwith "Unable to link against openblas."); let lapacke_conf = let disable_linking_flag = bgetenv "OWL_DISABLE_LAPACKE_LINKING_FLAG" in @@ -291,8 +289,7 @@ some details on how your openblas has been installed and the output of in if needs_lapacke_flag then - let open Base.Option.Monad_infix in - Base.Option.value + Option.value ~default:C.Pkg_config.{ cflags = []; libs = [ "-llapacke" ] } (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"llapacke") else default_pkg_config diff --git a/src/owl/config/dune b/src/owl/config/dune index a9a6574c7..cb623703a 100644 --- a/src/owl/config/dune +++ b/src/owl/config/dune @@ -1,3 +1,3 @@ (executable (name configure) - (libraries dune.configurator stdio)) + (libraries dune.configurator)) From fac592767cde07dc1421677006ec73202b7ad8fa Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Fri, 27 Oct 2023 10:31:38 +0200 Subject: [PATCH 20/22] add @jcreinhold patch for nonstandard setups See https://github.com/owlbarn/owl/pull/636 Signed-off-by: Marcello Seri --- src/owl/config/configure.ml | 89 ++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index 5ef8d4957..e908c2032 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -110,12 +110,20 @@ let get_os_type c = let get_ocaml_default_flags _c = [] + let get_ocaml_devmode_flags _c = let enable_devmode = bgetenv "OWL_ENABLE_DEVMODE" in if not enable_devmode then [] else [ "-w"; "-32-27-6-37-3" ] -let default_config c = +let clean_env_var env_var = + Sys.getenv env_var + |> String.trim + |> String.split_on_char ' ' + |> List.filter (fun s -> String.trim s <> "") + + +let get_default_config c = let os = let platform = C.C_define.import c ~includes:[ ] ~prelude:detect_system_os [ "PLATFORM_NAME", String ] in match List.map snd platform with @@ -136,12 +144,7 @@ let default_config c = | _ -> `unknown in let cflags = - try - Sys.getenv "OWL_CFLAGS" - |> String.trim - |> String.split_on_char ' ' - |> List.filter (fun s -> String.trim s <> "") - with + try clean_env_var "OWL_CFLAGS" with | Not_found -> [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] @ (match arch, os with @@ -163,7 +166,21 @@ let default_config c = in C.Pkg_config.{ cflags; libs } -let default_libs = [ "-lm" ] + +let default_cppflags = + try clean_env_var "OWL_CPPFLAGS" with + | Not_found -> [] + + +let default_ldflags = + try clean_env_var "OWL_LDFLAGS" with + | Not_found -> [] + + +let default_ldlibs = + try clean_env_var "OWL_LDLIBS" with + | Not_found -> [ "-lm" ] + let get_expmode_cflags _c = let enable_expmode = bgetenv "OWL_ENABLE_EXPMODE" in @@ -239,7 +256,7 @@ the output of `src/owl/config/configure.exe --verbose`. let () = C.main ~name:"owl" (fun c -> let (>>=) = Option.bind in - let default_config = default_config c in + let default_config = get_default_config c in let default_pkg_config = { C.Pkg_config.cflags = []; libs = [] } in let cblas_conf = Option.value ~default:default_pkg_config @@ -250,12 +267,30 @@ let () = ~default:openblas_default (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"openblas") in - if not - @@ C.c_test - c - test_linking - ~c_flags:openblas_conf.cflags - ~link_flags:(default_config.libs @ openblas_conf.libs) + let openmp_config = get_openmp_config c in + (* configure link options *) + let libs = + [] + @ default_ldflags + @ default_ldlibs + @ default_config.libs + @ openblas_conf.libs + @ cblas_conf.libs + @ default_gcc_path + @ get_accelerate_libs c + @ openmp_config.libs + in + (* configure compile options *) + let cflags = + [] + @ default_config.cflags + @ default_cppflags + @ openblas_conf.cflags + @ cblas_conf.cflags + @ get_expmode_cflags c + @ openmp_config.cflags + in + if not @@ C.c_test c test_linking ~c_flags:cflags ~link_flags:libs then ( Printf.printf {| @@ -294,29 +329,9 @@ some details on how your openblas has been installed and the output of (C.Pkg_config.get c >>= C.Pkg_config.query ~package:"llapacke") else default_pkg_config in - let openmp_config = get_openmp_config c in (* configure link options *) - let libs = - [] - @ default_config.libs - @ lapacke_conf.libs - @ openblas_conf.libs - @ cblas_conf.libs - @ default_libs - @ default_gcc_path - @ get_accelerate_libs c - @ openmp_config.libs - in - (* configure compile options *) - let cflags = - [ "-Wall"; "-pedantic"; "-Wextra"; "-Wunused" ] - @ lapacke_conf.cflags - @ openblas_conf.cflags - @ cblas_conf.cflags - @ default_config.cflags - @ get_expmode_cflags c - @ openmp_config.cflags - in + let libs = lapacke_conf.libs @ libs in + let cflags = lapacke_conf.cflags @ cflags in (* configure ocaml options *) let ocaml_flags = [] @ get_ocaml_default_flags c @ get_ocaml_devmode_flags c in (* assemble default config *) From 882021186c222a881c8b956715ab9d9c274c581b Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Tue, 31 Oct 2023 22:56:04 +0100 Subject: [PATCH 21/22] Update src/aeos/config/configure.ml --- src/aeos/config/configure.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aeos/config/configure.ml b/src/aeos/config/configure.ml index b028fcb4f..24549ef9f 100644 --- a/src/aeos/config/configure.ml +++ b/src/aeos/config/configure.ml @@ -96,7 +96,7 @@ let get_default_cflags c = in [ "-g"; "-O3"; "-Ofast" ] @ (match arch, os with - | `arm64, `mac -> [ "-mcpu=apple-m1" ] + | `arm64, `mac -> [ "-march=native" ] | `x86_64, _ -> [ "-march=native"; "-msse2" ] | _ -> []) @ [ "-funroll-loops"; "-ffast-math"; "-DSFMT_MEXP=19937"; "-fno-strict-aliasing" ] From d3b5390531a05b2a74754f46625a7f4d1efdb9b7 Mon Sep 17 00:00:00 2001 From: Marcello Seri Date: Tue, 31 Oct 2023 22:56:26 +0100 Subject: [PATCH 22/22] Update src/owl/config/configure.ml --- src/owl/config/configure.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/owl/config/configure.ml b/src/owl/config/configure.ml index e908c2032..18b12a70a 100644 --- a/src/owl/config/configure.ml +++ b/src/owl/config/configure.ml @@ -148,7 +148,7 @@ let get_default_config c = | Not_found -> [ (* Basic optimisation *) "-g"; "-O3"; "-Ofast" ] @ (match arch, os with - | `arm64, `mac -> [ "-mcpu=apple-m1" ] + | `arm64, `mac -> [ "-march=native" ] | `x86_64, _ -> [ "-march=native"; "-mfpmath=sse"; "-msse2" ] | _ -> []) @ [ (* Experimental switches, -ffast-math may break IEEE754 semantics*)