2012-05-18 25 views
7

在Linux中,可以启动一个进程(例如使用execve)并使其将特定内存区域用作堆栈空间?使用内存区域作为堆栈空间?

背景:

我有一个C++程序和快速的分配,让我“快速记忆”。我可以将它用于使用堆的对象并在快速内存中创建它们。精细。但我也有很多可变的生活在堆栈中。我怎样才能让他们使用快速记忆?

想法:实现一个“程序包装器”,它分配快速内存,然后启动实际的主程序,将指针传递给快速内存,程序将其用作堆栈。那可能吗?

[更新]

pthread安装似乎工作。

+3

我真的不认为你的* fast *分配器可以比堆栈分配更快。一般来说,堆栈分配每个函数需要几条指令。或者你的意思是说,内存比系统中其他任何地方的内存更快*。 –

+1

@DavidRodríguez-dribeas后者!它的内存很快,而不是分配器 – ritter

+1

你使用哪种平台,哪里有两种不同类型的RAM? –

回答

9

随着并行线程,你可以使用一个辅助线程程序逻辑,并设置使用pthread_attr_setstack()堆栈地址:

NAME 
     pthread_attr_setstack, pthread_attr_getstack - set/get stack 
     attributes in thread attributes object 

SYNOPSIS 
     #include <pthread.h> 

     int pthread_attr_setstack(pthread_attr_t *attr, 
           void *stackaddr, size_t stacksize); 

DESCRIPTION 
     The pthread_attr_setstack() function sets the stack address and 
     stack size attributes of the thread attributes object referred 
     to by attr to the values specified in stackaddr and stacksize, 
     respectively. These attributes specify the location and size 
     of the stack that should be used by a thread that is created 
     using the thread attributes object attr. 

     stackaddr should point to the lowest addressable byte of a buf‐ 
     fer of stacksize bytes that was allocated by the caller. The 
     pages of the allocated buffer should be both readable and 
     writable. 

我不遵循的是你如何期待得到任何通过做这样的事情来提升性能(我假设你的“快速”内存的目的是更好的性能)。

+2

太棒了!不知道它是否适用于我的情况,会试一试。你的问题:快速分配器是'cudaHostAlloc'返回页面锁定内存,用于加速内存传输到GPU。因此,如果这样做,我可以加速堆栈变量的副本! – ritter

+1

@Frank:有趣。让我们知道怎么回事。 – NPE