Skip to content

Commit 12d59f0

Browse files
committed
k+ reg tests
1 parent 1d6ae5d commit 12d59f0

7 files changed

Lines changed: 52 additions & 25 deletions

File tree

k+/blas.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@ uint64_t asum(uint64_t x){x=FZ(Fst(x));int32_t n=nn(x);if(tp(x)==Zt)n*=2;return
1515
uint64_t imax(uint64_t x){x=FZ(Fst(x));int32_t n=nn(x);int32_t i=tp(x)==Ft?cblas_idamax(n,FK(x),1):cblas_izamax(n,FK(x),1);return Ki(i);}
1616
uint64_t rot(uint64_t y){double c=f0(y),s=f1(y);uint64_t x=FZ(x2(y)),q=x3(y);dx(y);x=use(x);y=use(q);
1717
tp(x)==Zt?cblas_zdrot(nn(x),FK(x),1,FK(y),1,c,s):cblas_drot(nn(x),FK(x),1,FK(y),1,c,s);return l2(x,y);}
18-
uint64_t mv(uint64_t y){int32_t m,n; //todo..
19-
uint64_t A=FZ(x1(y)),x=FZ(x5(y)),r=FZ(x6(y));
20-
if(tp(A)==Zt){
21-
double a=f0(y),b=f4(y);int32_t rA=i2(y),op=i3(y);dx(y);r=use(r);dim(A,rA,&m,&n);
22-
cblas_dgemv(major(rA),trans(op),m,n,a,FK(A),n,FK(x),1,b,FK(r),1);
18+
uint64_t gemv(uint64_t u){int32_t m,n;
19+
// 0 1 2 3 4 5 6
20+
//y:gemm[a;A;rA;op;b;y;x]
21+
uint64_t A=FZ(x1(u)),y=FZ(x5(u)),x=FZ(x6(u));
22+
if(tp(A)==Ft){
23+
double a=f0(y),b=f4(y);int32_t rA=i2(y),op=i3(y);dx(u);y=use(y);dim(A,rA,&m,&n);
24+
cblas_dgemv(major(rA),trans(op),m,n,a,FK(A),n,FK(x),1,b,FK(y),1);
2325
dx(A);dx(x);return b;
2426
}else{
2527
printf("todo mv complex\n");
2628
}
2729
}
2830

29-
void regblas(void){ //encode names with: https://play.golang.org/p/4ethx6OEVCR
31+
void blas(void){ //encode names with: https://play.golang.org/p/4ethx6OEVCR
3032
reg(nrm2,"nrm2",1); //c-func k-name arity
3133
reg(asum,"asum",1);
3234
reg(imax,"imax",1);
3335
reg(rot, "rot",4);
34-
reg(mv, "mv",7);
36+
reg(gemv,"gemv",7);
3537
}
38+
__attribute((section("reg")))void*rblas=blas;

k+/curl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static size_t curlback(void *c,size_t n,size_t m,uint64_t*r){
77
uint64_t x=*r;int32_t xn=nn(x);
88
n*=m;x=uspc(x,Ct,n);*r=x;memcpy(M_+(int32_t)x+xn,c,n);return n;}
99

10-
uint64_t curl(uint64_t x){
10+
uint64_t kurl(uint64_t x){
1111
x=Fst(x);if(tp(x)!=Ct)trap();x=cat1(x,Kc(0));
1212
void*c=curl_easy_init();
1313
curl_easy_setopt(c,10002,M_+(int32_t)x);
@@ -19,4 +19,5 @@ uint64_t curl(uint64_t x){
1919
curl_easy_cleanup(c);
2020
dx(x);return r;}
2121

22-
void regcurl(void){reg(curl,"curl",1);}
22+
void curl(void){reg(kurl,"curl",1);}
23+
__attribute((section("reg")))void*rcurl=curl;

k+/l.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#define NX 128 //large enough to hold all external functions
1+
#define NX 64 //large enough to hold all external functions
22

33
typedef uint64_t (*XF)(uint64_t);
44
uint64_t IX=0;XF xtab[NX];
5-
uint64_t hlp=0,help=0;
65
void reg(XF f, const char*name, int32_t arity){
76
uint64_t c=mk(Ct,(int32_t)strlen(name));
87
if(!help){help=sc(Ku(1886152040ull));hlp=Key(mk(St,0),mk(It,0));}
@@ -12,17 +11,22 @@ void reg(XF f, const char*name, int32_t arity){
1211

1312
hlp=Cat(hlp,Key(Enl(s),Enl(Ki(arity))));
1413

14+
//a native function has type 14 and stores 2 values:
15+
// -any identifier, we use a primary function type 0 (no refcounts)
16+
// -a strings with the name used to display the function object
17+
//the identifier & the arglist is passed to native() when the function is called.
1518
int64_t l=ti(14,(uint32_t)l2(IX, c));
1619
SetI32((int32_t)l-12,arity);
17-
dx(Asn(s,l));
20+
dx(Asn(s,l)); //assign the function object to a global symbol, for the user to access it.
21+
if(NX==IX){fprintf(stderr,"xtab is too small: increase NX in l.c\n");exit(1);}
1822
xtab[IX++]=f;
1923
}
2024
int64_t cnative(int64_t x,int64_t y){return(int64_t)xtab[(int32_t)x](y);}
2125

2226

2327

2428
//some helpers to unpack arguments
25-
static uint64_t FZ(int64_t x){
29+
static uint64_t FZ(int64_t x){ //real or complex vector arg
2630
int32_t xt=tp(x);
2731
if(xt<16)trap();
2832
if(xt<Ft){x=Add(Kf(0.0),x);}

k+/l.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ void sincos(double,double*,double*);
66
static void sin1(int32_t,int32_t,int32_t);
77
static void cos1(int32_t,int32_t,int32_t);
88
#define cosin_(x,r,_) sincos(x,(double*)(M_+8+r),(double*)(M_+r));
9+
10+
//we collect names and arity of extention functions in a dict assigned to "help"
11+
uint64_t hlp=0,help=0;
12+
13+
//initialization magick, thanks @ovs.
14+
extern void(*__start_reg)(),(*__stop_reg)();

k+/readme

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
# BLAS /usr/include/x86_64-linux-gnu/cblas-netlib.h
1515
# nrm2[x] FZ l2 vector norm FZ mean double-float or complex vector
16-
# asum[x] FZ l1 vector norm
17-
# imax[x] FZ index of l1 max element
16+
# asum[x] FZ l1 vector norm some blas args are changed to facilitate projections
17+
# imax[x] FZ index of l1 max element (changing input last)
1818
# rot[c;s;x;y] c:f s:f x:FZ y:FZ plane rotation
19-
# mv[a;A;rA;op;b;x;y] c:fz A:FZ b:fz y:FZ a*A*x+b*y
19+
# dot dotc todo
20+
# gemv[a;A;rA;op;b;y;x] c:fz A:FZ b:fz y:FZ a*A*x+b*y
2021
#
2122
# CURL
2223
# curl"https://ktye.github.io/k.png"
@@ -28,23 +29,26 @@ set -x
2829
if [ "$1" = "get" ];then wget -Okc.orig https://github.com/ktye/i/releases/download/latest/k.c; fi
2930

3031
L=""
31-
R=""
32+
W=""
3233
awk -f m.awk kc.orig |\
3334
sed -e 's/math.h>$/math.h>\n#include"l.h"/' \
34-
-e 's/kinit()/kinit();libs()/' \
35+
-e 's/kinit()/&;for(void(**f)()=\&__start_reg;f<\&__stop_reg;(*f++)());dx(Asn(help,hlp))/' \
3536
>k.c
3637
cat m.c>>k.c
3738
cat l.c>>k.c
3839
# k.c
3940

40-
#these lines can be uncommented individually:
41-
cat blas.c>>k.c;R="$R regblas();";L="$L -lopenblas"
42-
cat curl.c>>k.c;R="$R regcurl();";L="$L -lcurl"
41+
42+
#these lines can be uncommented individually to exclude a package:
43+
cat blas.c>>k.c;L="$L -lopenblas";W="$W libopenblas.dll"
44+
cat curl.c>>k.c;L="$L -lcurl" ;W="$W libcurl-x64.dll"
4345
# k.c
4446

45-
echo "void libs(void){$R dx(Asn(help,hlp));}" >> k.c
47+
#type help to see a the list of all registered functions and their arity
4648

47-
K=-ok+
48-
C=cc
49+
if uname|grep Windows; then
50+
L="$W"; fi
4951
O="-O3 -march=native"
50-
$C $O $K k.c $L -lm
52+
C=cc
53+
K="-ok+ k.c"
54+
$C $O $K $L -lm

k+/tests/blas.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dim:(#;#*)@\ /
2+
A:4^1 2 3 4 2 1 2 1 0 3 5 2. /
3+
At:+A /
4+
A0:*At /
5+
dim A /4 3
6+
(nrm2 x)~%+/x*x:,/A /1
7+
+/A*A0 /30. 27. 13.
8+
gemv[1.;,/At;3;0;0;4#0.;A0] /30. 27. 13.

k+/tests/readme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$k+ *.t -e #run all tests and exit

0 commit comments

Comments
 (0)