scm2cpp-learn 0.22.0__py3-none-macosx_15_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. scm2cpp_learn/.dylibs/libgcc_s.1.1.dylib +0 -0
  2. scm2cpp_learn/.dylibs/libgfortran.5.dylib +0 -0
  3. scm2cpp_learn/.dylibs/libomp.dylib +0 -0
  4. scm2cpp_learn/.dylibs/libopenblasp-r0.3.34.dylib +0 -0
  5. scm2cpp_learn/.dylibs/libquadmath.0.dylib +0 -0
  6. scm2cpp_learn/__init__.py +4794 -0
  7. scm2cpp_learn/_classifiers.cpython-312-darwin.so +0 -0
  8. scm2cpp_learn/_compat.py +354 -0
  9. scm2cpp_learn/_generated/__init__.py +0 -0
  10. scm2cpp_learn/_generated/_loader.py +41 -0
  11. scm2cpp_learn/_generated/_loader_accel_cublas.py +185 -0
  12. scm2cpp_learn/_generated/_loader_classifiers.py +308 -0
  13. scm2cpp_learn/_generated/_loader_derived_blas.py +29 -0
  14. scm2cpp_learn/_generated/_loader_derived_cublas.py +29 -0
  15. scm2cpp_learn/_generated/_loader_derived_loops.py +29 -0
  16. scm2cpp_learn/_generated/_loader_gpu_cuda.py +313 -0
  17. scm2cpp_learn/_generated/_loader_gramops_blas.py +836 -0
  18. scm2cpp_learn/_generated/_loader_gramops_loops.py +855 -0
  19. scm2cpp_learn/_generated/_loader_nmf_blas.py +29 -0
  20. scm2cpp_learn/_generated/_loader_nmf_cublas.py +29 -0
  21. scm2cpp_learn/_generated/_loader_nmf_loops.py +29 -0
  22. scm2cpp_learn/_generated/accel_cublas/accel.cpp +26 -0
  23. scm2cpp_learn/_generated/accel_cublas/accel.hpp +1250 -0
  24. scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +82 -0
  25. scm2cpp_learn/_generated/classifiers.cpp +37 -0
  26. scm2cpp_learn/_generated/classifiers.hpp +3745 -0
  27. scm2cpp_learn/_generated/classifiers_capi.cpp +115 -0
  28. scm2cpp_learn/_generated/derived_blas/lasso_design.cpp +5 -0
  29. scm2cpp_learn/_generated/derived_blas/lasso_design.hpp +156 -0
  30. scm2cpp_learn/_generated/derived_blas/lasso_design_capi.cpp +19 -0
  31. scm2cpp_learn/_generated/derived_cublas/lasso_design.cpp +5 -0
  32. scm2cpp_learn/_generated/derived_cublas/lasso_design.hpp +158 -0
  33. scm2cpp_learn/_generated/derived_cublas/lasso_design_capi.cpp +19 -0
  34. scm2cpp_learn/_generated/derived_loops/lasso_design.cpp +5 -0
  35. scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +179 -0
  36. scm2cpp_learn/_generated/derived_loops/lasso_design_capi.cpp +19 -0
  37. scm2cpp_learn/_generated/gpu_cuda/gpu.cpp +37 -0
  38. scm2cpp_learn/_generated/gpu_cuda/gpu.hpp +3846 -0
  39. scm2cpp_learn/_generated/gpu_cuda/gpu_capi.cpp +308 -0
  40. scm2cpp_learn/_generated/gramops_blas/gramops.cpp +99 -0
  41. scm2cpp_learn/_generated/gramops_blas/gramops.hpp +6655 -0
  42. scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +295 -0
  43. scm2cpp_learn/_generated/gramops_loops/gramops.cpp +99 -0
  44. scm2cpp_learn/_generated/gramops_loops/gramops.hpp +7711 -0
  45. scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +301 -0
  46. scm2cpp_learn/_generated/lasso_cov.cpp +7 -0
  47. scm2cpp_learn/_generated/lasso_cov.hpp +152 -0
  48. scm2cpp_learn/_generated/lasso_cov_capi.cpp +25 -0
  49. scm2cpp_learn/_generated/nmf_blas/nmf_cd.cpp +5 -0
  50. scm2cpp_learn/_generated/nmf_blas/nmf_cd.hpp +132 -0
  51. scm2cpp_learn/_generated/nmf_blas/nmf_cd_capi.cpp +19 -0
  52. scm2cpp_learn/_generated/nmf_cublas/nmf_cd.cpp +5 -0
  53. scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +136 -0
  54. scm2cpp_learn/_generated/nmf_cublas/nmf_cd_capi.cpp +19 -0
  55. scm2cpp_learn/_generated/nmf_loops/nmf_cd.cpp +5 -0
  56. scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +167 -0
  57. scm2cpp_learn/_generated/nmf_loops/nmf_cd_capi.cpp +19 -0
  58. scm2cpp_learn/_generated/scm2cpp-blas.hpp +176 -0
  59. scm2cpp_learn/_generated/scm2cpp-cublas.hpp +155 -0
  60. scm2cpp_learn/_generated/scm2cpp-cuda.hpp +285 -0
  61. scm2cpp_learn/_generated/scm2cpp.hpp +1641 -0
  62. scm2cpp_learn/_gramops_blas.cpython-312-darwin.so +0 -0
  63. scm2cpp_learn/_lasso_cov.cpython-312-darwin.so +0 -0
  64. scm2cpp_learn/_lasso_derived_blas.cpython-312-darwin.so +0 -0
  65. scm2cpp_learn/_libfind.py +153 -0
  66. scm2cpp_learn/_nmf_blas.cpython-312-darwin.so +0 -0
  67. scm2cpp_learn/batch_capi.cu +431 -0
  68. scm2cpp_learn-0.22.0.dist-info/METADATA +1350 -0
  69. scm2cpp_learn-0.22.0.dist-info/RECORD +71 -0
  70. scm2cpp_learn-0.22.0.dist-info/WHEEL +6 -0
  71. scm2cpp_learn-0.22.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,136 @@
1
+
2
+ #ifndef NMF_CD_HPP
3
+ #define NMF_CD_HPP
4
+ #define SCM2CPP_MINIMAL
5
+ #include "scm2cpp-cublas.hpp"
6
+ #include <vector>
7
+ #include "scm2cpp.hpp"
8
+
9
+ double
10
+ nmf_half( scm2cpp::cspan<double> x, scm2cpp::span<double> w, scm2cpp::cspan<double> ht, std::int64_t n, std::int64_t p, std::int64_t k );
11
+ SCM2CPP_FN std::int64_t
12
+ nmf( scm2cpp::cspan<double> x, scm2cpp::cspan<double> xt, scm2cpp::span<double> w, scm2cpp::span<double> ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k );
13
+
14
+
15
+
16
+ inline double
17
+ nmf_half( scm2cpp::cspan<double> x, scm2cpp::span<double> w, scm2cpp::cspan<double> ht, std::int64_t n, std::int64_t p, std::int64_t k )
18
+ { scm2cpp::device_matrix d483 = scm2cpp::dmat_upload(ht, (p*k), 1);
19
+ if( k < p ) {
20
+ double violation = 0.0;
21
+ std::vector<double> b0471(k,0.0);
22
+ std::vector<double> g470((k*k),0.0);
23
+ std::vector<double> c469((n*k),0.0);
24
+ scm2cpp::device_matrix d481 = scm2cpp::dmat_upload(ht, (p*k), 1);
25
+ scm2cpp::device_matrix d482 = scm2cpp::dmat_upload(x, (n*p), 1);
26
+ scm2cpp::blas_syrk(g470, d481, 1.0, 0.0, 1, k, p) ;
27
+ scm2cpp::blas_gemm(c469, d482, d481, 1.0, 0.0, 0, 0, n, k, p) ;
28
+ scm2cpp::blas_gemm(c469, scm2cpp::dmat_upload(w, (n*k), 1), scm2cpp::dmat_upload(g470, (k*k), 1), -1.0, 1.0, 0, 0, n, k, k) ;
29
+ for( std::int64_t t = 0 ; !(t == k) ; t = (t+1) ) {
30
+ double hess = ([&]() -> double { std::int64_t i2063 = 0; double r2064 = 0.0; while (!(i2063 == p)) {
31
+ std::int64_t i2063_next487 = (i2063+1);
32
+ double r2064_next488 = (r2064+(ht[(((long long)i2063*k)+t)]*ht[(((long long)i2063*k)+t)]));
33
+ i2063 = i2063_next487;
34
+ r2064 = r2064_next488;
35
+ } return r2064; })();
36
+ for( std::int64_t i = 0 ; !(i == n) ; i = (i+1) ) {
37
+ double grad = 0.0;
38
+ double old = w[(((long long)i*k)+t)];
39
+ grad = (grad-c469[(((long long)i*k)+t)]) ;
40
+ double pg = ( ( old == 0.0 ) ? (( ( grad < 0.0 ) ? (grad) : (0.0) )) : (grad) );
41
+ violation = (violation+( ( pg < 0.0 ) ? ((0.0-pg)) : (pg) )) ;
42
+ ;
43
+ if( !(hess == 0.0) ) {
44
+ double wnew = (old-(grad/hess));
45
+ double wc = ( ( wnew < 0.0 ) ? (0.0) : (wnew) );
46
+ w[ (((long long)i*k)+t) ] = wc ;
47
+ if( !(wc == old) ) {
48
+ for( std::int64_t k473 = 0 ; !(k473 == k) ; k473 = (k473+1) ) {
49
+ c469[ (((long long)i*k)+k473) ] = (c469[(((long long)i*k)+k473)]-(g470[(((long long)t*k)+k473)]*(wc-old))) ;
50
+ } ;
51
+ } else {
52
+ false ;
53
+ } ;
54
+ ;
55
+ ;
56
+ } else {
57
+ false ;
58
+ } ;
59
+ ;
60
+ } ;
61
+ ;
62
+ } ;
63
+ 0 ;
64
+ ;
65
+ ;
66
+ return violation ;
67
+ }
68
+ else {
69
+ std::vector<double> r((n*p),0.0);
70
+ for( std::int64_t i2062 = 0 ; !(i2062 == (n*p)) ; i2062 = (i2062+1) ) {
71
+ r[ i2062 ] = x[i2062] ;
72
+ } ;
73
+ scm2cpp::blas_gemm(r, scm2cpp::dmat_upload(w, (n*k), 1), d483, -1.0, 1.0, 0, 1, n, p, k) ;
74
+ double violation = 0.0;
75
+ for( std::int64_t t = 0 ; !(t == k) ; t = (t+1) ) {
76
+ double hess = 0.0;
77
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
78
+ hess = (hess+(ht[(((long long)j*k)+t)]*ht[(((long long)j*k)+t)])) ;
79
+ } ;
80
+ for( std::int64_t i = 0 ; !(i == n) ; i = (i+1) ) {
81
+ double grad = 0.0;
82
+ double old = w[(((long long)i*k)+t)];
83
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
84
+ grad = (grad-(r[(((long long)i*p)+j)]*ht[(((long long)j*k)+t)])) ;
85
+ } ;
86
+ double pg = ( ( old == 0.0 ) ? (( ( grad < 0.0 ) ? (grad) : (0.0) )) : (grad) );
87
+ violation = (violation+( ( pg < 0.0 ) ? ((0.0-pg)) : (pg) )) ;
88
+ ;
89
+ if( !(hess == 0.0) ) {
90
+ double wnew = (old-(grad/hess));
91
+ double wc = ( ( wnew < 0.0 ) ? (0.0) : (wnew) );
92
+ w[ (((long long)i*k)+t) ] = wc ;
93
+ if( !(wc == old) ) {
94
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
95
+ r[ (((long long)i*p)+j) ] = (r[(((long long)i*p)+j)]-(ht[(((long long)j*k)+t)]*(wc-old))) ;
96
+ } ;
97
+ } else {
98
+ false ;
99
+ } ;
100
+ ;
101
+ ;
102
+ } else {
103
+ false ;
104
+ } ;
105
+ ;
106
+ } ;
107
+ ;
108
+ } ;
109
+ return violation ;
110
+ }
111
+ }
112
+
113
+
114
+ SCM2CPP_FN inline std::int64_t
115
+ nmf( scm2cpp::cspan<double> x, scm2cpp::cspan<double> xt, scm2cpp::span<double> w, scm2cpp::span<double> ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k )
116
+ { double vinit = 0.0;
117
+ std::int64_t stop = 0;
118
+ std::int64_t it = 0;
119
+ for( ; !((stop == 1 || it >= iters)) ; ) {
120
+ it = (it+1) ;
121
+ double v = (nmf_half(x,w,ht,n,p,k)+nmf_half(xt,ht,w,p,n,k));
122
+ if( it == 1 ) {
123
+ vinit = v ;
124
+ } else {
125
+ 0 ;
126
+ } ;
127
+ if( (vinit == 0.0 || (v/vinit) <= tol) ) {
128
+ stop = 1 ;
129
+ } else {
130
+ 0 ;
131
+ } ;
132
+ ;
133
+ } ;
134
+ return it ;
135
+ }
136
+ #endif // NMF_CD_HPP
@@ -0,0 +1,19 @@
1
+ // extern "C" wrappers over the translated functions, for Python and
2
+ // any other caller that speaks the C ABI. Array parameters arrive as
3
+ // element pointers: a view parameter takes the pointer as it is, so
4
+ // the caller's buffer is read and written in place with no copy; a
5
+ // fixed-extent one is reinterpreted as the std::array the function
6
+ // expects. The caller guarantees the length either way.
7
+ // Build (boost includes only if the generated header asks for them,
8
+ // which a numeric kernel's does not):
9
+ // g++ -O2 -std=c++17 -shared -fPIC -I. -o libnmf_cd.so nmf_cd_capi.cpp
10
+ #include "nmf_cd.hpp"
11
+ #include <vector>
12
+ #include <algorithm>
13
+
14
+ extern "C" double scm2cpp_nmf_half(double* x, double* w, double* ht, std::int64_t n, std::int64_t p, std::int64_t k) {
15
+ return nmf_half(x, w, ht, n, p, k);
16
+ }
17
+ extern "C" std::int64_t scm2cpp_nmf(double* x, double* xt, double* w, double* ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k) {
18
+ return nmf(x, xt, w, ht, iters, tol, n, p, k);
19
+ }
@@ -0,0 +1,5 @@
1
+
2
+ #include "nmf_cd.hpp"
3
+ // #include "scm2cpp.hpp"
4
+
5
+
@@ -0,0 +1,167 @@
1
+
2
+ #ifndef NMF_CD_HPP
3
+ #define NMF_CD_HPP
4
+ #define SCM2CPP_MINIMAL
5
+ #include <vector>
6
+ #include "scm2cpp.hpp"
7
+
8
+ double
9
+ nmf_half( scm2cpp::cspan<double> x, scm2cpp::span<double> w, scm2cpp::cspan<double> ht, std::int64_t n, std::int64_t p, std::int64_t k );
10
+ SCM2CPP_FN std::int64_t
11
+ nmf( scm2cpp::cspan<double> x, scm2cpp::cspan<double> xt, scm2cpp::span<double> w, scm2cpp::span<double> ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k );
12
+
13
+
14
+
15
+ inline double
16
+ nmf_half( scm2cpp::cspan<double> x, scm2cpp::span<double> w, scm2cpp::cspan<double> ht, std::int64_t n, std::int64_t p, std::int64_t k )
17
+ { if( k < p ) {
18
+ double violation = 0.0;
19
+ std::vector<double> b0471(k,0.0);
20
+ std::vector<double> g470((k*k),0.0);
21
+ std::vector<double> c469((n*k),0.0);
22
+ for( std::int64_t y2069 = 0 ; !(y2069 == k) ; y2069 = (y2069+1) ) {
23
+ for( std::int64_t y2070 = 0 ; !(y2070 == k) ; y2070 = (y2070+1) ) {
24
+ g470[ (((long long)y2069*k)+y2070) ] = ([&]() -> double { std::int64_t k2071 = 0; double sum2081 = 0.0; while (!(k2071 == p)) {
25
+ std::int64_t k2071_next481 = (k2071+1);
26
+ double sum2081_next482 = (sum2081+(ht[(((long long)k2071*k)+y2070)]*ht[(((long long)k2071*k)+y2069)]));
27
+ k2071 = k2071_next481;
28
+ sum2081 = sum2081_next482;
29
+ } return sum2081; })() ;
30
+ } ;
31
+ } ;
32
+ for( std::int64_t y2072 = 0 ; !(y2072 == n) ; y2072 = (y2072+1) ) {
33
+ for( std::int64_t y2073 = 0 ; !(y2073 == k) ; y2073 = (y2073+1) ) {
34
+ c469[ (((long long)y2072*k)+y2073) ] = ([&]() -> double { std::int64_t k2074 = 0; double sum2083 = 0.0; while (!(k2074 == p)) {
35
+ std::int64_t k2074_next483 = (k2074+1);
36
+ double sum2083_next484 = (sum2083+(x[(((long long)y2072*p)+k2074)]*ht[(((long long)k2074*k)+y2073)]));
37
+ k2074 = k2074_next483;
38
+ sum2083 = sum2083_next484;
39
+ } return sum2083; })() ;
40
+ } ;
41
+ } ;
42
+ for( std::int64_t y2075 = 0 ; !(y2075 == n) ; y2075 = (y2075+1) ) {
43
+ for( std::int64_t y2076 = 0 ; !(y2076 == k) ; y2076 = (y2076+1) ) {
44
+ c469[ (((long long)y2075*k)+y2076) ] = (c469[(((long long)y2075*k)+y2076)]-([&]() -> double { std::int64_t k2077 = 0; double sum2085 = 0.0; while (!(k2077 == k)) {
45
+ std::int64_t k2077_next485 = (k2077+1);
46
+ double sum2085_next486 = (sum2085+(w[(((long long)y2075*k)+k2077)]*g470[(((long long)k2077*k)+y2076)]));
47
+ k2077 = k2077_next485;
48
+ sum2085 = sum2085_next486;
49
+ } return sum2085; })()) ;
50
+ } ;
51
+ } ;
52
+ for( std::int64_t t = 0 ; !(t == k) ; t = (t+1) ) {
53
+ double hess = ([&]() -> double { std::int64_t i2078 = 0; double r2079 = 0.0; while (!(i2078 == p)) {
54
+ std::int64_t i2078_next487 = (i2078+1);
55
+ double r2079_next488 = (r2079+(ht[(((long long)i2078*k)+t)]*ht[(((long long)i2078*k)+t)]));
56
+ i2078 = i2078_next487;
57
+ r2079 = r2079_next488;
58
+ } return r2079; })();
59
+ for( std::int64_t i = 0 ; !(i == n) ; i = (i+1) ) {
60
+ double grad = 0.0;
61
+ double old = w[(((long long)i*k)+t)];
62
+ grad = (grad-c469[(((long long)i*k)+t)]) ;
63
+ double pg = ( ( old == 0.0 ) ? (( ( grad < 0.0 ) ? (grad) : (0.0) )) : (grad) );
64
+ violation = (violation+( ( pg < 0.0 ) ? ((0.0-pg)) : (pg) )) ;
65
+ ;
66
+ if( !(hess == 0.0) ) {
67
+ double wnew = (old-(grad/hess));
68
+ double wc = ( ( wnew < 0.0 ) ? (0.0) : (wnew) );
69
+ w[ (((long long)i*k)+t) ] = wc ;
70
+ if( !(wc == old) ) {
71
+ for( std::int64_t k473 = 0 ; !(k473 == k) ; k473 = (k473+1) ) {
72
+ c469[ (((long long)i*k)+k473) ] = (c469[(((long long)i*k)+k473)]-(g470[(((long long)t*k)+k473)]*(wc-old))) ;
73
+ } ;
74
+ } else {
75
+ false ;
76
+ } ;
77
+ ;
78
+ ;
79
+ } else {
80
+ false ;
81
+ } ;
82
+ ;
83
+ } ;
84
+ ;
85
+ } ;
86
+ 0 ;
87
+ ;
88
+ return violation ;
89
+ }
90
+ else {
91
+ std::vector<double> r((n*p),0.0);
92
+ for( std::int64_t i2062 = 0 ; !(i2062 == (n*p)) ; i2062 = (i2062+1) ) {
93
+ r[ i2062 ] = x[i2062] ;
94
+ } ;
95
+ for( std::int64_t k2063 = 0 ; !(k2063 == n) ; k2063 = (k2063+1) ) {
96
+ for( std::int64_t k2064 = 0 ; !(k2064 == p) ; k2064 = (k2064+1) ) {
97
+ r[ (((long long)k2063*p)+k2064) ] = (r[(((long long)k2063*p)+k2064)]-([&]() -> double { std::int64_t i2066 = 0; double r2067 = 0.0; while (!(i2066 == k)) {
98
+ std::int64_t i2066_next489 = (i2066+1);
99
+ double r2067_next490 = (r2067+(w[(((long long)k2063*k)+i2066)]*ht[(((long long)k2064*k)+i2066)]));
100
+ i2066 = i2066_next489;
101
+ r2067 = r2067_next490;
102
+ } return r2067; })()) ;
103
+ } ;
104
+ } ;
105
+ double violation = 0.0;
106
+ for( std::int64_t t = 0 ; !(t == k) ; t = (t+1) ) {
107
+ double hess = 0.0;
108
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
109
+ hess = (hess+(ht[(((long long)j*k)+t)]*ht[(((long long)j*k)+t)])) ;
110
+ } ;
111
+ for( std::int64_t i = 0 ; !(i == n) ; i = (i+1) ) {
112
+ double grad = 0.0;
113
+ double old = w[(((long long)i*k)+t)];
114
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
115
+ grad = (grad-(r[(((long long)i*p)+j)]*ht[(((long long)j*k)+t)])) ;
116
+ } ;
117
+ double pg = ( ( old == 0.0 ) ? (( ( grad < 0.0 ) ? (grad) : (0.0) )) : (grad) );
118
+ violation = (violation+( ( pg < 0.0 ) ? ((0.0-pg)) : (pg) )) ;
119
+ ;
120
+ if( !(hess == 0.0) ) {
121
+ double wnew = (old-(grad/hess));
122
+ double wc = ( ( wnew < 0.0 ) ? (0.0) : (wnew) );
123
+ w[ (((long long)i*k)+t) ] = wc ;
124
+ if( !(wc == old) ) {
125
+ for( std::int64_t j = 0 ; !(j == p) ; j = (j+1) ) {
126
+ r[ (((long long)i*p)+j) ] = (r[(((long long)i*p)+j)]-(ht[(((long long)j*k)+t)]*(wc-old))) ;
127
+ } ;
128
+ } else {
129
+ false ;
130
+ } ;
131
+ ;
132
+ ;
133
+ } else {
134
+ false ;
135
+ } ;
136
+ ;
137
+ } ;
138
+ ;
139
+ } ;
140
+ return violation ;
141
+ }
142
+ }
143
+
144
+
145
+ SCM2CPP_FN inline std::int64_t
146
+ nmf( scm2cpp::cspan<double> x, scm2cpp::cspan<double> xt, scm2cpp::span<double> w, scm2cpp::span<double> ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k )
147
+ { double vinit = 0.0;
148
+ std::int64_t stop = 0;
149
+ std::int64_t it = 0;
150
+ for( ; !((stop == 1 || it >= iters)) ; ) {
151
+ it = (it+1) ;
152
+ double v = (nmf_half(x,w,ht,n,p,k)+nmf_half(xt,ht,w,p,n,k));
153
+ if( it == 1 ) {
154
+ vinit = v ;
155
+ } else {
156
+ 0 ;
157
+ } ;
158
+ if( (vinit == 0.0 || (v/vinit) <= tol) ) {
159
+ stop = 1 ;
160
+ } else {
161
+ 0 ;
162
+ } ;
163
+ ;
164
+ } ;
165
+ return it ;
166
+ }
167
+ #endif // NMF_CD_HPP
@@ -0,0 +1,19 @@
1
+ // extern "C" wrappers over the translated functions, for Python and
2
+ // any other caller that speaks the C ABI. Array parameters arrive as
3
+ // element pointers: a view parameter takes the pointer as it is, so
4
+ // the caller's buffer is read and written in place with no copy; a
5
+ // fixed-extent one is reinterpreted as the std::array the function
6
+ // expects. The caller guarantees the length either way.
7
+ // Build (boost includes only if the generated header asks for them,
8
+ // which a numeric kernel's does not):
9
+ // g++ -O2 -std=c++17 -shared -fPIC -I. -o libnmf_cd.so nmf_cd_capi.cpp
10
+ #include "nmf_cd.hpp"
11
+ #include <vector>
12
+ #include <algorithm>
13
+
14
+ extern "C" double scm2cpp_nmf_half(double* x, double* w, double* ht, std::int64_t n, std::int64_t p, std::int64_t k) {
15
+ return nmf_half(x, w, ht, n, p, k);
16
+ }
17
+ extern "C" std::int64_t scm2cpp_nmf(double* x, double* xt, double* w, double* ht, std::int64_t iters, double tol, std::int64_t n, std::int64_t p, std::int64_t k) {
18
+ return nmf(x, xt, w, ht, iters, tol, n, p, k);
19
+ }
@@ -0,0 +1,176 @@
1
+ // The contractions of the array algebra as cblas calls: what the ops
2
+ // of bindings/cblas-binding.scm expand to under --blas.
3
+ //
4
+ // Every array is a flat row-major container of doubles (std::vector,
5
+ // boost::array, anything with operator[] over contiguous storage).
6
+ // A contraction whose summed axes sit at the front or the back of
7
+ // each operand is one dgemm over the matrices the other axes flatten
8
+ // to: ta says the left operand is stored k x m (op(a) = a^T) rather
9
+ // than m x k, tb that the right one is stored n x k rather than k x n.
10
+ // Link with -lopenblas or another cblas.
11
+ #ifndef SCM2CPP_BLAS_HPP
12
+ #define SCM2CPP_BLAS_HPP
13
+
14
+ // SCM2CPP_MKL: Intel MKL's names for the same two interfaces (the
15
+ // package's setup.py defines it when it links MKL)
16
+ // MSVC's C++ has no C99 _Complex, which lapack.h falls back to; it
17
+ // takes std::complex when lapack_complex_* are defined first (nothing
18
+ // here calls a complex routine, so their layout never matters)
19
+ #if defined(_MSC_VER) && !defined(lapack_complex_float)
20
+ #include <complex>
21
+ #define lapack_complex_float std::complex<float>
22
+ #define lapack_complex_double std::complex<double>
23
+ #endif
24
+ #ifdef SCM2CPP_MKL
25
+ #include <mkl_cblas.h>
26
+ #include <mkl_lapacke.h>
27
+ #else
28
+ #include <cblas.h>
29
+ #include <lapacke.h>
30
+ #endif
31
+ #include <cmath>
32
+ #include <cstddef>
33
+ #include <vector>
34
+
35
+ namespace scm2cpp {
36
+
37
+ // y (m x n) = alpha op(a) op(b) + beta y. A product whose m or n is 1
38
+ // at run time (one class of a logistic regression, say) is a dgemv: the
39
+ // vector operand is contiguous either way, and OpenBLAS's dgemm with a
40
+ // unit dimension costs a third more than its dgemv.
41
+ template <class Y, class A, class B>
42
+ inline void blas_gemm(Y& y, const A& a, const B& b, double alpha, double beta,
43
+ int ta, int tb, int m, int n, int k) {
44
+ if (n == 1)
45
+ cblas_dgemv(CblasRowMajor, ta ? CblasTrans : CblasNoTrans, ta ? k : m, ta ? m : k,
46
+ alpha, &a[0], ta ? m : k, &b[0], 1, beta, &y[0], 1);
47
+ else if (m == 1)
48
+ cblas_dgemv(CblasRowMajor, tb ? CblasNoTrans : CblasTrans, tb ? n : k, tb ? k : n,
49
+ alpha, &b[0], tb ? k : n, &a[0], 1, beta, &y[0], 1);
50
+ else
51
+ cblas_dgemm(CblasRowMajor, ta ? CblasTrans : CblasNoTrans,
52
+ tb ? CblasTrans : CblasNoTrans, m, n, k,
53
+ alpha, &a[0], ta ? m : k, &b[0], tb ? k : n, beta, &y[0], n);
54
+ }
55
+
56
+ // y (m) = alpha op(a) v + beta y (v k)
57
+ template <class Y, class A, class V>
58
+ inline void blas_gemv(Y& y, const A& a, const V& v, double alpha, double beta,
59
+ int ta, int m, int k) {
60
+ if (ta)
61
+ cblas_dgemv(CblasRowMajor, CblasTrans, k, m, alpha, &a[0], m, &v[0], 1,
62
+ beta, &y[0], 1);
63
+ else
64
+ cblas_dgemv(CblasRowMajor, CblasNoTrans, m, k, alpha, &a[0], k, &v[0], 1,
65
+ beta, &y[0], 1);
66
+ }
67
+
68
+ // batch slabs of the same: y[l] (m x n) = alpha op(a[l]) op(b[l]) +
69
+ // beta y[l] for l below batch, the slabs contiguous (strides m*k,
70
+ // k*n, m*n): a contraction whose leading labels are shared by both
71
+ // operands and the result
72
+ template <class Y, class A, class B>
73
+ inline void blas_gemm_batched(Y& y, const A& a, const B& b, double alpha, double beta,
74
+ int ta, int tb, int m, int n, int k, int batch) {
75
+ for (int l = 0; l < batch; ++l)
76
+ cblas_dgemm(CblasRowMajor, ta ? CblasTrans : CblasNoTrans,
77
+ tb ? CblasTrans : CblasNoTrans, m, n, k,
78
+ alpha, &a[(size_t)l * m * k], ta ? m : k,
79
+ &b[(size_t)l * k * n], tb ? k : n, beta, &y[(size_t)l * m * n], n);
80
+ }
81
+
82
+ // y (m x m) = alpha op(a) op(a)^T + beta y: the Gram matrix, one
83
+ // triangle by dsyrk and the other mirrored
84
+ template <class Y, class A>
85
+ inline void blas_syrk(Y& y, const A& a, double alpha, double beta, int ta,
86
+ int m, int k) {
87
+ cblas_dsyrk(CblasRowMajor, CblasUpper, ta ? CblasTrans : CblasNoTrans, m, k,
88
+ alpha, &a[0], ta ? m : k, beta, &y[0], m);
89
+ for (int i = 1; i < m; ++i)
90
+ for (int j = 0; j < i; ++j)
91
+ y[i * m + j] = y[j * m + i];
92
+ }
93
+
94
+ // The symmetric eigenproblem as sym-eigen.scm states it: A (m x m,
95
+ // symmetric, row-major) is destroyed, V gets the eigenvectors as
96
+ // columns (V[j m + c] is entry j of vector c), EV the eigenvalues
97
+ // (ascending here; the callers order them themselves). dsyevd, the
98
+ // divide and conquer that numpy's eigh runs, leaves the vectors in A
99
+ // in exactly that layout. Returns 0, or LAPACK's info.
100
+ template <class A, class V, class E>
101
+ inline int lapack_syevd(A& a, V& v, E& ev, int m) {
102
+ int info = LAPACKE_dsyevd(LAPACK_ROW_MAJOR, 'V', 'U', m, &a[0], m, &ev[0]);
103
+ for (std::size_t t = 0; t < (std::size_t)m * m; ++t) v[t] = a[t];
104
+ return info;
105
+ }
106
+
107
+ // The K largest eigenpairs as sym-eigen.scm's sym-eigen-top states
108
+ // them: A (m x m) destroyed, EV the values in decreasing order, V (m x
109
+ // K) the vectors as columns. dsyevr over the index range m-K+1..m (what
110
+ // scipy's eigh with subset_by_index runs), which gives them increasing;
111
+ // reversed here. Returns LAPACK's info.
112
+ template <class A, class V, class E>
113
+ inline int lapack_syevr_top(A& a, V& v, E& ev, int m, int k) {
114
+ if (k <= 0) return 0;
115
+ std::vector<double> w(m), z((std::size_t)m * k);
116
+ std::vector<lapack_int> isuppz(2 * (std::size_t)k);
117
+ lapack_int found = 0;
118
+ int info = LAPACKE_dsyevr(LAPACK_ROW_MAJOR, 'V', 'I', 'U', m, &a[0], m, 0.0, 0.0,
119
+ m - k + 1, m, 0.0, &found, w.data(), z.data(), k, isuppz.data());
120
+ for (int c = 0; c < k; ++c) {
121
+ ev[c] = w[k - 1 - c];
122
+ for (int j = 0; j < m; ++j) v[(std::size_t)j * k + c] = z[(std::size_t)j * k + (k - 1 - c)];
123
+ }
124
+ return info;
125
+ }
126
+
127
+ // A v = lam B v as sym-eigen.scm's sym-eigen-gen states it: A and B
128
+ // (m x m) destroyed, V the vectors as columns with V'BV = I, EV the
129
+ // values (increasing). dsygvd, itype 1, what scipy's eigh(a, b) runs.
130
+ // Returns LAPACK's info.
131
+ template <class A, class B, class V, class E>
132
+ inline int lapack_sygvd(A& a, B& b, V& v, E& ev, int m) {
133
+ int info = LAPACKE_dsygvd(LAPACK_ROW_MAJOR, 1, 'V', 'U', m, &a[0], m, &b[0], m, &ev[0]);
134
+ for (std::size_t t = 0; t < (std::size_t)m * m; ++t) v[t] = a[t];
135
+ return info;
136
+ }
137
+
138
+ // A W = B as gram.scm's spd-solve states it: A (m x m, positive
139
+ // definite) destroyed, B (m x q, row-major) overwritten by W. dposv,
140
+ // what numpy's solve on a positive definite matrix and scipy's
141
+ // cho_solve reach. Returns LAPACK's info (> 0: not positive definite).
142
+ template <class A, class B>
143
+ inline int lapack_posv(A& a, B& b, int m, int q) {
144
+ return LAPACKE_dposv(LAPACK_ROW_MAJOR, 'U', m, q, &a[0], m, &b[0], q);
145
+ }
146
+
147
+ // gram.scm's spd-solve-rcond: lapack_posv, then the reciprocal 1-norm
148
+ // condition number of A from the factor dposv leaves (dpocon, the
149
+ // estimate scipy's solve warns by); -1 when A is not positive definite.
150
+ template <class A, class B>
151
+ inline double lapack_posv_rcond(A& a, B& b, int m, int q) {
152
+ double anorm = 0.0;
153
+ for (int c = 0; c < m; ++c) {
154
+ double s = 0.0;
155
+ for (int i = 0; i < m; ++i) s += std::fabs(a[(std::size_t)i * m + c]);
156
+ if (s > anorm) anorm = s;
157
+ }
158
+ if (LAPACKE_dposv(LAPACK_ROW_MAJOR, 'U', m, q, &a[0], m, &b[0], q) != 0) return -1.0;
159
+ double rc = 0.0;
160
+ if (LAPACKE_dpocon(LAPACK_ROW_MAJOR, 'U', m, &a[0], m, anorm, &rc) != 0) return -1.0;
161
+ return rc;
162
+ }
163
+
164
+ // The thin SVD as sym-svd.scm states it: A (m x n, row-major) is
165
+ // destroyed, S gets the k = min(m, n) values in decreasing order, U the
166
+ // m x k left vectors, VT the k x n right ones. dgesdd, the divide and
167
+ // conquer that numpy's svd runs, with jobz 'S'. Returns LAPACK's info.
168
+ template <class A, class U, class S, class VT>
169
+ inline int lapack_gesdd(A& a, U& u, S& s, VT& vt, int m, int n) {
170
+ int k = m < n ? m : n;
171
+ return LAPACKE_dgesdd(LAPACK_ROW_MAJOR, 'S', m, n, &a[0], n, &s[0], &u[0], k, &vt[0], n);
172
+ }
173
+
174
+ } // namespace scm2cpp
175
+
176
+ #endif
@@ -0,0 +1,155 @@
1
+ // The contractions of the array algebra as cuBLAS calls: what the
2
+ // ops of bindings/cublas-binding.scm expand to under --cublas.
3
+ //
4
+ // The matrix operand of every product is a device_matrix, the copy
5
+ // of a row-major p x n host matrix that dmat_upload takes once at the
6
+ // entry of the scope using it; the vector operands are host
7
+ // containers, copied over per call and the result copied back. A
8
+ // row-major p x n host matrix is the column-major n x p matrix cuBLAS
9
+ // reads (leading dimension n), so nothing is ever transposed in
10
+ // memory. The one cuBLAS handle is created on first use.
11
+ // Compile with -I<cuda>/include, link with -lcublas -lcudart.
12
+ #ifndef SCM2CPP_CUBLAS_HPP
13
+ #define SCM2CPP_CUBLAS_HPP
14
+
15
+ #include <cublas_v2.h>
16
+ #include <cuda_runtime.h>
17
+ #include <cstdio>
18
+ #include <cstdlib>
19
+ #include <memory>
20
+
21
+ namespace scm2cpp {
22
+
23
+ inline void cuda_check(cudaError_t e, const char* what) {
24
+ if (e != cudaSuccess) {
25
+ std::fprintf(stderr, "scm2cpp-cublas: %s: %s\n", what, cudaGetErrorString(e));
26
+ std::abort();
27
+ }
28
+ }
29
+ inline void cublas_check(cublasStatus_t s, const char* what) {
30
+ if (s != CUBLAS_STATUS_SUCCESS) {
31
+ std::fprintf(stderr, "scm2cpp-cublas: %s: status %d\n", what, (int)s);
32
+ std::abort();
33
+ }
34
+ }
35
+
36
+ inline cublasHandle_t cublas_handle() {
37
+ static cublasHandle_t h = 0;
38
+ if (!h) cublas_check(cublasCreate(&h), "cublasCreate");
39
+ return h;
40
+ }
41
+
42
+ // A device copy of len doubles, freed with the last handle to it.
43
+ struct device_buffer {
44
+ std::shared_ptr<double> d;
45
+ size_t len;
46
+ device_buffer() : len(0) {}
47
+ explicit device_buffer(size_t n) : len(n) {
48
+ double* p = 0;
49
+ cuda_check(cudaMalloc((void**)&p, n * sizeof(double)), "cudaMalloc");
50
+ d.reset(p, cudaFree);
51
+ }
52
+ double* get() const { return d.get(); }
53
+ template <class V>
54
+ void upload(const V& v) {
55
+ cuda_check(cudaMemcpy(get(), &v[0], len * sizeof(double), cudaMemcpyHostToDevice),
56
+ "cudaMemcpy H2D");
57
+ }
58
+ template <class V>
59
+ void download(V& v) const {
60
+ cuda_check(cudaMemcpy(&v[0], get(), len * sizeof(double), cudaMemcpyDeviceToHost),
61
+ "cudaMemcpy D2H");
62
+ }
63
+ };
64
+
65
+ template <class V>
66
+ inline device_buffer to_device(const V& v, size_t n) {
67
+ device_buffer b(n);
68
+ b.upload(v);
69
+ return b;
70
+ }
71
+
72
+ // A row-major p x n matrix on the device.
73
+ struct device_matrix {
74
+ device_buffer buf;
75
+ int p, n;
76
+ device_matrix() : p(0), n(0) {}
77
+ device_matrix(const device_buffer& b, int p_, int n_) : buf(b), p(p_), n(n_) {}
78
+ double* get() const { return buf.get(); }
79
+ };
80
+
81
+ template <class X>
82
+ inline device_matrix dmat_upload(const X& x, int p, int n) {
83
+ return device_matrix(to_device(x, (size_t)p * n), p, n);
84
+ }
85
+
86
+ // y (m x n host) = alpha op(a) op(b) + beta y, a and b on the device.
87
+ // A row-major m x n array is the column-major n x m array of the same
88
+ // buffer, so the row-major product is the column-major product of the
89
+ // operands swapped: C^T = op(b)^T op(a)^T.
90
+ template <class Y>
91
+ inline void blas_gemm(Y& y, const device_matrix& a, const device_matrix& b,
92
+ double alpha, double beta, int ta, int tb, int m, int n, int k) {
93
+ device_buffer dy = beta != 0.0 ? to_device(y, (size_t)m * n)
94
+ : device_buffer((size_t)m * n);
95
+ cublas_check(cublasDgemm(cublas_handle(), tb ? CUBLAS_OP_T : CUBLAS_OP_N,
96
+ ta ? CUBLAS_OP_T : CUBLAS_OP_N, n, m, k,
97
+ &alpha, b.get(), tb ? k : n, a.get(), ta ? m : k,
98
+ &beta, dy.get(), n),
99
+ "cublasDgemm");
100
+ dy.download(y);
101
+ }
102
+
103
+ // batch slabs of the same in one call: y[l] (m x n) = alpha op(a[l])
104
+ // op(b[l]) + beta y[l], the slabs contiguous (strides m*k, k*n, m*n)
105
+ template <class Y>
106
+ inline void blas_gemm_batched(Y& y, const device_matrix& a, const device_matrix& b,
107
+ double alpha, double beta, int ta, int tb,
108
+ int m, int n, int k, int batch) {
109
+ const size_t total = (size_t)m * n * batch;
110
+ device_buffer dy = beta != 0.0 ? to_device(y, total) : device_buffer(total);
111
+ cublas_check(cublasDgemmStridedBatched(
112
+ cublas_handle(), tb ? CUBLAS_OP_T : CUBLAS_OP_N,
113
+ ta ? CUBLAS_OP_T : CUBLAS_OP_N, n, m, k,
114
+ &alpha, b.get(), tb ? k : n, (long long)k * n,
115
+ a.get(), ta ? m : k, (long long)m * k,
116
+ &beta, dy.get(), n, (long long)m * n, batch),
117
+ "cublasDgemmStridedBatched");
118
+ dy.download(y);
119
+ }
120
+
121
+ // y (m host) = alpha op(a) v + beta y (a device, v k host)
122
+ template <class Y, class V>
123
+ inline void blas_gemv(Y& y, const device_matrix& a, const V& v, double alpha,
124
+ double beta, int ta, int m, int k) {
125
+ device_buffer dv = to_device(v, k);
126
+ device_buffer dy = beta != 0.0 ? to_device(y, m) : device_buffer(m);
127
+ if (ta)
128
+ cublas_check(cublasDgemv(cublas_handle(), CUBLAS_OP_N, m, k, &alpha, a.get(), m,
129
+ dv.get(), 1, &beta, dy.get(), 1), "cublasDgemv");
130
+ else
131
+ cublas_check(cublasDgemv(cublas_handle(), CUBLAS_OP_T, k, m, &alpha, a.get(), k,
132
+ dv.get(), 1, &beta, dy.get(), 1), "cublasDgemv");
133
+ dy.download(y);
134
+ }
135
+
136
+ // y (m x m host) = alpha op(a) op(a)^T + beta y: one triangle by dsyrk
137
+ // over the column-major reading, the other mirrored on the host
138
+ template <class Y>
139
+ inline void blas_syrk(Y& y, const device_matrix& a, double alpha, double beta,
140
+ int ta, int m, int k) {
141
+ device_buffer dy = beta != 0.0 ? to_device(y, (size_t)m * m)
142
+ : device_buffer((size_t)m * m);
143
+ cublas_check(cublasDsyrk(cublas_handle(), CUBLAS_FILL_MODE_UPPER,
144
+ ta ? CUBLAS_OP_N : CUBLAS_OP_T, m, k, &alpha, a.get(),
145
+ ta ? m : k, &beta, dy.get(), m),
146
+ "cublasDsyrk");
147
+ dy.download(y);
148
+ for (int i = 0; i < m; ++i)
149
+ for (int j = i + 1; j < m; ++j)
150
+ y[i * m + j] = y[j * m + i];
151
+ }
152
+
153
+ } // namespace scm2cpp
154
+
155
+ #endif