r****t 发帖数: 10904 | 1 有没有比较巧妙的 macro 可以 unroll for loop 呢? compiler #pragma unroll 失
败了,手工 unroll 发现很值得。
我写了这个,但是编译的时候错误是:
error: identifier "forloop" is undefined
#define forloop(n) if (n>0) do_something_with_n(n) ; forloop(n-1) |
t****t 发帖数: 6806 | 2 you can't do recursive expansion with C macro. CPP specifically forbid this,
even if you use several nested macro.
you can achieve nesting through including the file itself. boost provides
some "functions". you may want to read
http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/index.html
but this is dangerous for inexperienced programmers. for example, even if
macro can recursively expand, your forloop(5) will expand to something like:
if (5>0) f(5); if (5-1>0) f(5-1); if (5-1-1>0) f(5-
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有没有比较巧妙的 macro 可以 unroll for loop 呢? compiler #pragma unroll 失 : 败了,手工 unroll 发现很值得。 : 我写了这个,但是编译的时候错误是: : error: identifier "forloop" is undefined : #define forloop(n) if (n>0) do_something_with_n(n) ; forloop(n-1)
|
d****p 发帖数: 685 | 3 You can do that with boost's BOOST_PP_REPEAT in boost preprocessor lib.
sth like
#define RUN_FUNC_IN_LOOP(unused, loopCount, funcToRun) funcToRun(n);
where funcToRun looks like void funcToRun(int n)
So the loop is BOOST_PP_REPEAT(1000, RUN_FUNC_IN_LOOP, yourFunc)
But it is a bad idea since the code is unreadable to most people.And if the
n is large ...
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有没有比较巧妙的 macro 可以 unroll for loop 呢? compiler #pragma unroll 失 : 败了,手工 unroll 发现很值得。 : 我写了这个,但是编译的时候错误是: : error: identifier "forloop" is undefined : #define forloop(n) if (n>0) do_something_with_n(n) ; forloop(n-1)
|
r****t 发帖数: 10904 | 4 啊。。cpp 原来不让 recusion 的?cpp 不够灵活,template class 有内存上的问题
,写起
来不容易, compiler (nvcc) 还不一定支持。搜 "preprocessor loop" 很多都指向
boost 这个 preprossesor, 不过这部分程序没法用 boost, 看来还是用个 template
engine 来生成代码好了。
this,
provides
if
like:
and
variables.
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : you can't do recursive expansion with C macro. CPP specifically forbid this, : even if you use several nested macro. : you can achieve nesting through including the file itself. boost provides : some "functions". you may want to read : http://www.boost.org/doc/libs/1_43_0/libs/preprocessor/doc/index.html : but this is dangerous for inexperienced programmers. for example, even if : macro can recursively expand, your forloop(5) will expand to something like: : if (5>0) f(5); if (5-1>0) f(5-1); if (5-1-1>0) f(5-
|
t****t 发帖数: 6806 | 5 why it can not use boost? it's just a CPP library, should work on whatever
compiler.
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 啊。。cpp 原来不让 recusion 的?cpp 不够灵活,template class 有内存上的问题 : ,写起 : 来不容易, compiler (nvcc) 还不一定支持。搜 "preprocessor loop" 很多都指向 : boost 这个 preprossesor, 不过这部分程序没法用 boost, 看来还是用个 template : engine 来生成代码好了。 : : this, : provides : if : like:
|
r****t 发帖数: 10904 | 6 This is nvcc for gpu from nvidia. For kernel code (the computation code)
my understanding is that even STL is not available. It support some c++
feature like overloading, templates and classes without device memory
members, however, mostly the kernel code is in c89.
whatever
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : why it can not use boost? it's just a CPP library, should work on whatever : compiler.
|
t****t 发帖数: 6806 | 7 i said it's CPP library. you know what is CPP, don't you?
it has nothing to do with c++ or c or whatever. you can write visual basic
with it.
probably it's too twisted for you anyway. this is not designed for entry
level user.
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : This is nvcc for gpu from nvidia. For kernel code (the computation code) : my understanding is that even STL is not available. It support some c++ : feature like overloading, templates and classes without device memory : members, however, mostly the kernel code is in c89. : : whatever
|
r****t 发帖数: 10904 | 8 i did not get it that cpp is enough for this to work. thanks for
reminding, I'll have a look when I get a time.
Currently template engine works ok for me. It's very powerful when dealing
with C code: I can evaluate much of expressions in compile time even for
floats, while templates only do int for non-typed argument, and hence do
not build different instantiations for different non-int const arguments.
basic
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : i said it's CPP library. you know what is CPP, don't you? : it has nothing to do with c++ or c or whatever. you can write visual basic : with it. : probably it's too twisted for you anyway. this is not designed for entry : level user.
|
t****t 发帖数: 6806 | 9 one correction: for large part of boost, the user do *not* need to link to a
library because they are header only, although most of boost are built on
templates. same for STL.
i once read the document of nvcc, it seems they only implement a subset of
template (i guess template is quite complex for compiler writer in any case)
. so i guess still no boost or STL, unfortunately. correct me if i am wrong.
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : i did not get it that cpp is enough for this to work. thanks for : reminding, I'll have a look when I get a time. : Currently template engine works ok for me. It's very powerful when dealing : with C code: I can evaluate much of expressions in compile time even for : floats, while templates only do int for non-typed argument, and hence do : not build different instantiations for different non-int const arguments. : : basic
|
j******n 发帖数: 271 | 10 If the current tool is not sufficient, find another tool to help. An ugly
but working solution is to add some scripts to your code.
e. g.
// a.h.in:
#include "more.h"
// my macros
`echo "#define MACRO_0(f) f(0)"; i=0; while [ $i -lt 10 ] ; do j=$((i + 1));
echo "#define MACRO_$j(f) MACRO_$i(f); f($j)" ; i=$j; done`
// Makefile:
a.h: a.h.in
cat a.h.in | (echo "cat - < a.h
would generate your macros in a.h:
#include "more.h"
// my macros
#define MACRO_0 f(0)
#d |
l******e 发帖数: 12192 | 11 可以用template unroll,很neat
不过,过度unroll不是好事
【在 r****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 有没有比较巧妙的 macro 可以 unroll for loop 呢? compiler #pragma unroll 失 : 败了,手工 unroll 发现很值得。 : 我写了这个,但是编译的时候错误是: : error: identifier "forloop" is undefined : #define forloop(n) if (n>0) do_something_with_n(n) ; forloop(n-1)
|
r****t 发帖数: 10904 | 12 Only function templates are implemented. I did not test seriously if class template works, just so far so good. But since class cannot have virtual methods, there's not much juice
using classes.
a
case)
wrong.
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : one correction: for large part of boost, the user do *not* need to link to a : library because they are header only, although most of boost are built on : templates. same for STL. : i once read the document of nvcc, it seems they only implement a subset of : template (i guess template is quite complex for compiler writer in any case) : . so i guess still no boost or STL, unfortunately. correct me if i am wrong.
|
r****t 发帖数: 10904 | 13 I call this a DIY template engine method... :)
);
【在 j******n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : If the current tool is not sufficient, find another tool to help. An ugly : but working solution is to add some scripts to your code. : e. g. : // a.h.in: : #include "more.h" : // my macros : `echo "#define MACRO_0(f) f(0)"; i=0; while [ $i -lt 10 ] ; do j=$((i + 1)); : echo "#define MACRO_$j(f) MACRO_$i(f); f($j)" ; i=$j; done` : // Makefile: : a.h: a.h.in
|