Changeset: 63378d373390 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=63378d373390
Added Files:
        sql/backends/monet5/UDF/capi/Tests/capi08.sql
        sql/backends/monet5/UDF/capi/Tests/capi08.stable.err
        sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
Modified Files:
        sql/backends/monet5/UDF/capi/Tests/All
        sql/backends/monet5/UDF/capi/capi.c
Branch: jitudf
Log Message:

Add function cache to avoid recompiling functions constantly.


diffs (truncated from 780 to 300 lines):

diff --git a/sql/backends/monet5/UDF/capi/Tests/All 
b/sql/backends/monet5/UDF/capi/Tests/All
--- a/sql/backends/monet5/UDF/capi/Tests/All
+++ b/sql/backends/monet5/UDF/capi/Tests/All
@@ -7,3 +7,4 @@ capi04
 capi05
 capi06
 capi07
+capi08
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi08.sql 
b/sql/backends/monet5/UDF/capi/Tests/capi08.sql
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi08.sql
@@ -0,0 +1,34 @@
+# test caching behavior
+
+START TRANSACTION;
+
+CREATE FUNCTION capi08(inp INTEGER) RETURNS INTEGER LANGUAGE C {
+    result->initialize(result, inp.count);
+    for(size_t i = 0; i < inp.count; i++) {
+        result->data[i] = inp.data[i] * 2;
+    }
+};
+
+CREATE TABLE integers(i INTEGER);
+INSERT INTO integers VALUES (1), (2), (3), (4), (5);
+
+SELECT capi08(i) FROM integers;
+# this function should be loaded from the cache
+SELECT capi08(i) FROM integers;
+
+DROP FUNCTION capi08;
+
+CREATE FUNCTION capi08(inp DOUBLE) RETURNS DOUBLE LANGUAGE C {
+    result->initialize(result, inp.count);
+    for(size_t i = 0; i < inp.count; i++) {
+        result->data[i] = inp.data[i] * 2;
+    }
+};
+
+# same code and same function name, but different parameters
+# this needs to be recompiled now
+SELECT capi08(i) FROM integers;
+SELECT capi08(i) FROM integers;
+
+
+ROLLBACK;
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi08.stable.err 
b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.err
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.err
@@ -0,0 +1,34 @@
+stderr of test 'capi08` in directory 'sql/backends/monet5/UDF/capi` itself:
+
+
+# 16:28:17 >  
+# 16:28:17 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=37555" "--set" 
"mapi_usock=/var/tmp/mtest-59671/.s.monetdb.37555" "--set" "monet_prompt=" 
"--forcemito" "--dbpath=NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi"
+# 16:28:17 >  
+
+# builtin opt  gdk_dbpath = /Users/myth/opt/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = no
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 37555
+# cmdline opt  mapi_usock = /var/tmp/mtest-59671/.s.monetdb.37555
+# cmdline opt  monet_prompt = 
+# cmdline opt  gdk_dbpath = 
/Users/myth/opt/mTests/sql/backends/monet5/UDF/capi/NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi
+# cmdline opt  gdk_debug = 536870922
+
+# 16:28:17 >  
+# 16:28:17 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-59671" "--port=37555"
+# 16:28:17 >  
+
+
+# 16:28:18 >  
+# 16:28:18 >  "Done."
+# 16:28:18 >  
+
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out 
b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
@@ -0,0 +1,124 @@
+stdout of test 'capi08` in directory 'sql/backends/monet5/UDF/capi` itself:
+
+
+# 16:28:17 >  
+# 16:28:17 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=37555" "--set" 
"mapi_usock=/var/tmp/mtest-59671/.s.monetdb.37555" "--set" "monet_prompt=" 
"--forcemito" "--dbpath=NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi"
+# 16:28:17 >  
+
+# MonetDB 5 server v11.28.0
+# This is an unreleased version
+# Serving database 'mTests_sql_backends_monet5_UDF_capi', using 4 threads
+# Compiled for x86_64-apple-darwin15.6.0/64bit with 128bit integers
+# Found 8.000 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2017 MonetDB B.V., all rights reserved
+# Visit https://www.monetdb.org/ for further information
+# Listening for connection requests on 
mapi:monetdb://dhcp-21.eduroam.cwi.nl:37555/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-59671/.s.monetdb.37555
+# MonetDB/SQL module loaded
+
+Ready.
+# SQL catalog created, loading sql scripts once
+# loading sql script: 09_like.sql
+# loading sql script: 10_math.sql
+# loading sql script: 11_times.sql
+# loading sql script: 12_url.sql
+# loading sql script: 13_date.sql
+# loading sql script: 14_inet.sql
+# loading sql script: 15_querylog.sql
+# loading sql script: 16_tracelog.sql
+# loading sql script: 17_temporal.sql
+# loading sql script: 18_index.sql
+# loading sql script: 20_vacuum.sql
+# loading sql script: 21_dependency_functions.sql
+# loading sql script: 22_clients.sql
+# loading sql script: 23_skyserver.sql
+# loading sql script: 25_debug.sql
+# loading sql script: 26_sysmon.sql
+# loading sql script: 27_rejects.sql
+# loading sql script: 39_analytics.sql
+# loading sql script: 39_analytics_hge.sql
+# loading sql script: 40_json.sql
+# loading sql script: 40_json_hge.sql
+# loading sql script: 41_md5sum.sql
+# loading sql script: 45_uuid.sql
+# loading sql script: 46_profiler.sql
+# loading sql script: 51_sys_schema_extension.sql
+# loading sql script: 60_wlcr.sql
+# loading sql script: 72_fits.sql
+# loading sql script: 74_netcdf.sql
+# loading sql script: 75_storagemodel.sql
+# loading sql script: 80_statistics.sql
+# loading sql script: 80_udf.sql
+# loading sql script: 80_udf_hge.sql
+# loading sql script: 90_generator.sql
+# loading sql script: 90_generator_hge.sql
+# loading sql script: 99_system.sql
+
+# 16:28:17 >  
+# 16:28:17 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-59671" "--port=37555"
+# 16:28:17 >  
+
+#START TRANSACTION;
+#CREATE FUNCTION capi08(inp INTEGER) RETURNS INTEGER LANGUAGE C {
+#    result->initialize(result, inp.count);
+#    for(size_t i = 0; i < inp.count; i++) {
+#        result->data[i] = inp.data[i] * 2;
+#    }
+#};
+#CREATE TABLE integers(i INTEGER);
+#INSERT INTO integers VALUES (1), (2), (3), (4), (5);
+[ 5    ]
+#SELECT capi08(i) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% int # type
+% 2 # length
+[ 2    ]
+[ 4    ]
+[ 6    ]
+[ 8    ]
+[ 10   ]
+#SELECT capi08(i) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% int # type
+% 2 # length
+[ 2    ]
+[ 4    ]
+[ 6    ]
+[ 8    ]
+[ 10   ]
+#DROP FUNCTION capi08;
+#CREATE FUNCTION capi08(inp DOUBLE) RETURNS DOUBLE LANGUAGE C {
+#    result->initialize(result, inp.count);
+#    for(size_t i = 0; i < inp.count; i++) {
+#        result->data[i] = inp.data[i] * 2;
+#    }
+#};
+#SELECT capi08(i) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% double # type
+% 24 # length
+[ 2    ]
+[ 4    ]
+[ 6    ]
+[ 8    ]
+[ 10   ]
+#SELECT capi08(i) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% double # type
+% 24 # length
+[ 2    ]
+[ 4    ]
+[ 6    ]
+[ 8    ]
+[ 10   ]
+#ROLLBACK;
+
+# 16:28:18 >  
+# 16:28:18 >  "Done."
+# 16:28:18 >  
+
diff --git a/sql/backends/monet5/UDF/capi/capi.c 
b/sql/backends/monet5/UDF/capi/capi.c
--- a/sql/backends/monet5/UDF/capi/capi.c
+++ b/sql/backends/monet5/UDF/capi/capi.c
@@ -30,6 +30,24 @@ typedef struct _allocated_region {
 static __thread allocated_region *allocated_regions;
 static __thread jmp_buf jump_buffer;
 
+typedef char *(*jitted_function)(void **inputs, void **outputs,
+                                                                
malloc_function_ptr malloc);
+
+struct _cached_functions;
+typedef struct _cached_functions {
+       jitted_function function;
+       BUN expression_hash;
+       char* parameters;
+       void* dll_handle;
+       struct _cached_functions *next;
+} cached_functions;
+
+#define FUNCTION_CACHE_SIZE 128
+
+static cached_functions *function_cache[FUNCTION_CACHE_SIZE];
+static MT_Lock cache_lock;
+
+
 static str CUDFeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci,
                                        bit grouped);
 
@@ -46,12 +64,10 @@ str CUDFevalAggr(Client cntxt, MalBlkPtr
 str CUDFprelude(void *ret)
 {
        (void)ret;
+       MT_lock_init(&cache_lock, "cache_lock");
        return MAL_SUCCEED;
 }
 
-typedef char *(*jitted_function)(void **inputs, void **outputs,
-                                                                
malloc_function_ptr malloc);
-
 static bool WriteDataToFile(FILE *f, const void *data, size_t data_size)
 {
        fwrite(data, data_size, 1, f);
@@ -278,12 +294,11 @@ static str CUDFeval(Client cntxt, MalBlk
        str *args = NULL;
        str *output_names = NULL;
        char *msg = MAL_SUCCEED;
-       char *funcname = "yet_another_c_function";
        node *argnode;
        int seengrp = FALSE;
        FILE *f = NULL;
        void *handle = NULL;
-       jitted_function func;
+       jitted_function func = NULL;
        int ret;
 
        FILE *compiler = NULL;
@@ -315,6 +330,11 @@ static str CUDFeval(Client cntxt, MalBlk
                                : (GDKgetenv(cc_flag) ? GDKgetenv(cc_flag) : 
JIT_COMPILER_NAME);
 
        const char *struct_prefix = "struct cudf_data_struct_";
+       const char* funcname;
+
+       BUN expression_hash = 0, funcname_hash = 0;
+       cached_functions* cached_function;
+       char* function_parameters = NULL;
 
        (void)cntxt;
 
@@ -342,6 +362,8 @@ static str CUDFeval(Client cntxt, MalBlk
                sqlfun = *(sql_func **)getArgReference(stk, pci, pci->retc);
        }
 
+       funcname = sqlfun ? sqlfun->base.name : "yet_another_c_function";
+
        args = (str *)GDKzalloc(sizeof(str) * pci->argc);
        output_names =
                pci->retc > 0 ? (str *)GDKzalloc(sizeof(str) * pci->retc) : 
NULL;
@@ -375,6 +397,7 @@ static str CUDFeval(Client cntxt, MalBlk
                        argnode = argnode->next;
                }
        }
+
        // name unnamed outputs
        for (i = 0; i < (size_t)pci->retc; i++) {
                if (!output_names[i]) {
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to