Replace OpenMP.id, OpenMP.numThreads with threadpool.id(), threadpool.size().

parent f04aca16
......@@ -251,16 +251,18 @@ L1 cache misses and power consumption. This multiprocessing is achieved using th
OpenMP standard which is natively supported by most C compilers, rather than using
a non-portable threading library.
### OpenMP.numThreads : Number
### threadpool.size() => Number
(Wraps `omp_get_num_threads()` if that means something to you.)
This is the number of physical threads that are active. Right now, that is fixed
and equal to the number of logical cores.
### OpenMP.id : Number
Each thread is assigned an `id`, which runs from 0 to OpenMP.numThreads - 1.
### threadpool.id() => Number
(Wraps `omp_get_thread_num()` if that means something to you.)
Each thread is assigned an `id`, which runs from 0 to threadpool.size() - 1.
If for some reason you don't want to run multiple instances (e.g.
for a command line utility that reads from stdin and writes to stdout) add a
check at the beginning of your function and only run it if `OpenMP.id == 0`.
check at the beginning of your function and only run it if `threadpool.id() === 0`.
## Event Loop
......
......@@ -6,7 +6,7 @@ function (args) {
var timers = Queues.timers;
var compile = modules.compile;
if (args.OpenMP.id !== 0) return
if (args.threadpool.id() !== 0) return
var queue = []
......
function (o) {
o.stdio.print(o.OpenMP.id + '/' + o.OpenMP.numThreads)
o.stdio.print(o.threadpool.id() + '/' + o.threadpool.size())
}
......@@ -2,7 +2,7 @@
// dukroll (https://gitlab.com/dukboot/dukroll)
import pull from 'pull-stream'
if (imports.OpenMP.id === 0) {
if (imports.threadpool.id() === 0) {
var c = pull.count()
c(null, imports.stdio.print)
c(null, imports.stdio.print)
......
function (o) {
// For fun, let every thread race to read from stdin. Quite amusing.
// Results will (probably) be printed out of order.
var c;
var thread = o.threadpool.id()
var c
while (true) {
c = o.stdio.getchar()
if (c instanceof Error) break
o.stdio.print(o.OpenMP.id + ': ' + c)
o.stdio.print(thread + ': ' + c)
}
}
......@@ -22,7 +22,7 @@ function (o) {
return true
}
if (o.OpenMP.id === 0) {
if (o.threadpool.id() === 0) {
var c
var chars = []
read(null, function next(end, data) {
......
function (o) {
// If you want to read bytes in order, I would recommend having
// a single master thread read the bytes. You can divide the work later.
if (o.OpenMP.id === 0) {
var thread = o.threadpool.id()
if (thread === 0) {
var c;
while (true) {
c = o.stdio.getchar()
o.stdio.print(o.OpenMP.id + ': ' + c)
o.stdio.print(thread + ': ' + c)
if (c instanceof Error) break
o.modules.sleep(100)
}
......
function (o) {
// If you want to read bytes in order, I would recommend having
// a single master thread read the bytes. You can divide the work later.
if (o.OpenMP.id === 0) {
var thread = o.threadpool.id()
if (thread === 0) {
var UTF8decoder = new TextDecoder('utf-8')
var c
var buffer = new Uint8Array(8);
......@@ -10,7 +11,7 @@ function (o) {
if (c instanceof Error) break
if (c === undefined) o.modules.sleep(100)
if (c > 0) {
o.stdio.print(o.OpenMP.id + ': read ' + c + ' bytes')
o.stdio.print(thread + ': read ' + c + ' bytes')
o.stdio.print(UTF8decoder.decode(buffer.subarray(0, c)))
}
}
......
function (o) {
if (o.OpenMP.id === 0) {
if (o.threadpool.id() === 0) {
// This runs in about 550ms on my machine.
// Interestingly, I get the same performance buffering "manually" like this,
// as I do by leaving stdout buffering enabled in the C layer and running
......
function (o) {
if (o.OpenMP.id === 0) {
if (o.threadpool.id() === 0) {
// On my system, this takes 18.0 seconds to run!
// This is because by default we are not buffering output.
// See `stdout-buffered` example next.
......
function (o) {
// If you want to read bytes in order, I would recommend having
// a single master thread read the bytes. You can divide the work later.
if (o.OpenMP.id === 0) {
if (o.threadpool.id() === 0) {
var c;
var chars = [];
while (true) {
......
......@@ -22,7 +22,7 @@ function (o) {
return true
}
if (o.OpenMP.id === 0) {
if (o.threadpool.id() === 0) {
var unicodeStream = o.modules.compile('unicode-stream.js')
var readUnicode = unicodeStream(read)
var c
......
#include "duktape/duktape.h"
#include "bindings_openmp.h"
void app_push_openmp(duk_context *ctx) {
duk_push_object(ctx);
duk_push_c_function(ctx, wrap_omp_get_thread_num, 0);
duk_put_prop_string(ctx, -2, "id");
duk_push_c_function(ctx, wrap_omp_get_num_threads, 0);
duk_put_prop_string(ctx, -2, "size");
}
duk_ret_t wrap_omp_get_thread_num(duk_context *ctx) {
duk_push_int(ctx, omp_get_thread_num());
return 1;
}
duk_ret_t wrap_omp_get_num_threads(duk_context *ctx) {
duk_push_int(ctx, omp_get_num_threads());
return 1;
}
......@@ -76,14 +76,10 @@ int main(int argc, char *argv[] /* char *environ[] */) {
}
// Construct the prime argument
// OpenMP
duk_push_object(ctx);
duk_push_object(ctx);
duk_push_int(ctx, omp_get_thread_num());
duk_put_prop_string(ctx, -2, "id");
duk_push_int(ctx, omp_get_num_threads());
duk_put_prop_string(ctx, -2, "numThreads");
duk_put_prop_string(ctx, -2, "OpenMP");
// Provide access to some OpenMP thread info
app_push_openmp(ctx);
duk_put_prop_string(ctx, -2, "threadpool");
// Provide access to the environment
duk_push_object(ctx);
duk_push_c_function(ctx, c_argv, 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment