由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 如何用 preprocessor unroll for loop?
相关主题
师傅们,C++概念题,弟子有礼了CPP 大拿看看这段代码写的咋样
CPP questioninline functions in C++
What syntax is this?(about macro)嵌入式编程问题
Visual studio: 2008: create a macro in compiler configuration[合集] C代码这么写,居然没有错。
问低级问题size of structure
基于macro的meta programming真难懂Segmentation fault
c++ thread 求助Question on using ## in #define
c++ include里的list definition跟boost list不一样三个C syntax 弱问题
相关话题的讨论汇总
话题: unroll话题: macro话题: boost话题: loop话题: cpp
进入Programming版参与讨论
1 (共1页)
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 的大作中提到】
: 有没有比较巧妙的 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 的大作中提到】
: 有没有比较巧妙的 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 的大作中提到】
: 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 的大作中提到】
: 啊。。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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
: 有没有比较巧妙的 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 的大作中提到】
: 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 的大作中提到】
: 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

1 (共1页)
进入Programming版参与讨论
相关主题
三个C syntax 弱问题问低级问题
visual studio 2005怎么不让自动添加#pragma once?基于macro的meta programming真难懂
#ifdef, #ifndef 这些 preprocessor 应该叫什么呀?c++ thread 求助
一个简单的小问题c++ include里的list definition跟boost list不一样
师傅们,C++概念题,弟子有礼了CPP 大拿看看这段代码写的咋样
CPP questioninline functions in C++
What syntax is this?(about macro)嵌入式编程问题
Visual studio: 2008: create a macro in compiler configuration[合集] C代码这么写,居然没有错。
相关话题的讨论汇总
话题: unroll话题: macro话题: boost话题: loop话题: cpp