Skip to content

Commit

Permalink
fix zenofx asm volatile invalid in omp context
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jul 15, 2021
1 parent 03681d2 commit d720eb0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
O=arts/testifelse.zsg
O=arts/ZFXv2.zsg

default: run

Expand All @@ -7,6 +7,10 @@ dist: all
./dist.sh

all:
cmake -B build
make -C build -j `python -c 'from multiprocessing import cpu_count; print(cpu_count() * 2)'`

release_all:
cmake -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/tmp-install
make -C build -j `python -c 'from multiprocessing import cpu_count; print(cpu_count() * 2)'`

Expand Down
9 changes: 4 additions & 5 deletions ZFX/include/zfx/x64.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ struct Executable {
float locals[SimdWidth * 256];

void execute() {
auto entry = (void (*)())exec->mem;
asm volatile (
"call *%0"
"call *(%%rax)"
:
: "" (entry)
, "c" ((uintptr_t)(void *)exec->consts)
, "d" ((uintptr_t)(void *)locals)
: "a" (&exec->mem)
, "c" ((uintptr_t)(void *)&exec->consts[0])
, "d" ((uintptr_t)(void *)&locals[0])
: "cc", "memory"
);
}
Expand Down
139 changes: 138 additions & 1 deletion arts/ZFXv2.zsg
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@
"control"
]
},
"BreakFor": {
"inputs": [
"FOR",
"SRC",
"COND"
],
"outputs": [
"DST"
],
"params": [],
"categories": [
"control"
]
},
"CachedOnce": {
"inputs": [
"input",
Expand Down Expand Up @@ -445,6 +459,93 @@
"list"
]
},
"ExtractSmallDict": {
"inputs": [
"dict",
"SRC",
"COND"
],
"outputs": [
"obj0",
"obj1",
"obj2",
"obj3",
"DST"
],
"params": [
[
"string",
"name0",
""
],
[
"string",
"name1",
""
],
[
"string",
"name2",
""
],
[
"string",
"name3",
""
]
],
"categories": [
"dict"
]
},
"FuncBegin": {
"inputs": [
"extraArgs",
"SRC",
"COND"
],
"outputs": [
"args",
"FUNC",
"DST"
],
"params": [],
"categories": [
"functional"
]
},
"FuncCall": {
"inputs": [
"function",
"args",
"SRC",
"COND"
],
"outputs": [
"rets",
"DST"
],
"params": [],
"categories": [
"functional"
]
},
"FuncEnd": {
"inputs": [
"rets",
"FUNC",
"SRC",
"COND"
],
"outputs": [
"function",
"DST"
],
"params": [],
"categories": [
"functional"
]
},
"GetFrameNum": {
"inputs": [
"SRC",
Expand Down Expand Up @@ -515,6 +616,23 @@
"frame"
]
},
"IfElse": {
"inputs": [
"true",
"false",
"cond",
"SRC",
"COND"
],
"outputs": [
"result",
"DST"
],
"params": [],
"categories": [
"control"
]
},
"ImportObjPrimitive": {
"inputs": [
"path",
Expand Down Expand Up @@ -620,7 +738,7 @@
[
"int",
"value",
"0"
"42"
]
],
"categories": [
Expand Down Expand Up @@ -1519,6 +1637,25 @@
"primitive"
]
},
"PrintMessage": {
"inputs": [
"SRC",
"COND"
],
"outputs": [
"DST"
],
"params": [
[
"string",
"message",
"hello"
]
],
"categories": [
"debug"
]
},
"PrintNumeric": {
"inputs": [
"value",
Expand Down
24 changes: 13 additions & 11 deletions projects/ZenoFX/pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <zfx/x64.h>
#include <cassert>

static zfx::Compiler<zfx::x64::Program> compiler;
static zfx::Compiler compiler;
static zfx::x64::Assembler assembler;

struct Buffer {
float *base = nullptr;
Expand All @@ -17,7 +18,7 @@ struct Buffer {
};

static void vectors_wrangle
( zfx::Program<zfx::x64::Program> *prog
( zfx::x64::Executable *exec
, std::vector<Buffer> const &chs
) {
if (chs.size() == 0)
Expand All @@ -28,20 +29,20 @@ static void vectors_wrangle
}

#pragma omp parallel for
for (int i = 0; i < size - prog->SimdWidth + 1; i += prog->SimdWidth) {
auto ctx = prog->make_context();
for (int i = 0; i < size - exec->SimdWidth + 1; i += exec->SimdWidth) {
auto ctx = exec->make_context();
for (int j = 0; j < chs.size(); j++) {
for (int k = 0; k < prog->SimdWidth; k++)
for (int k = 0; k < exec->SimdWidth; k++)
ctx.channel(j)[k] = chs[j].base[chs[j].stride * (i + k)];
}
ctx.execute();
for (int j = 0; j < chs.size(); j++) {
for (int k = 0; k < prog->SimdWidth; k++)
for (int k = 0; k < exec->SimdWidth; k++)
chs[j].base[chs[j].stride * (i + k)] = ctx.channel(j)[k];
}
}
for (int i = size / prog->SimdWidth * prog->SimdWidth; i < size; i++) {
auto ctx = prog->make_context();
for (int i = size / exec->SimdWidth * exec->SimdWidth; i < size; i++) {
auto ctx = exec->make_context();
for (int j = 0; j < chs.size(); j++) {
ctx.channel(j)[0] = chs[j].base[chs[j].stride * i];
}
Expand All @@ -57,7 +58,7 @@ struct ParticlesWrangle : zeno::INode {
auto prim = get_input<zeno::PrimitiveObject>("prim");
auto code = get_input<zeno::StringObject>("zfxCode")->get();

zfx::Options opts;
zfx::Options opts(zfx::Options::for_x64);
for (auto const &[key, attr]: prim->m_attrs) {
int dim = std::visit([] (auto const &v) {
using T = std::decay_t<decltype(v[0])>;
Expand Down Expand Up @@ -96,6 +97,7 @@ struct ParticlesWrangle : zeno::INode {
}

auto prog = compiler.compile(code, opts);
auto exec = assembler.assemble(prog->assembly);

std::vector<float> pars(prog->params.size());
for (int i = 0; i < pars.size(); i++) {
Expand All @@ -106,7 +108,7 @@ struct ParticlesWrangle : zeno::INode {
parnames.end(), std::pair{name, dimid});
auto value = parvals.at(it - parnames.begin());
printf("(valued %f)\n", value);
prog->parameter(prog->param_id(name, dimid)) = value;
exec->parameter(prog->param_id(name, dimid)) = value;
}

std::vector<Buffer> chs(prog->symbols.size());
Expand All @@ -123,7 +125,7 @@ struct ParticlesWrangle : zeno::INode {
}, attr);
chs[i] = iob;
}
vectors_wrangle(prog, chs);
vectors_wrangle(exec, chs);

set_output("prim", std::move(prim));
}
Expand Down

0 comments on commit d720eb0

Please sign in to comment.