[R] aligned memory allocation in C

Luke Tierney luke at stat.uiowa.edu
Wed Aug 13 14:30:26 CEST 2008


On Wed, 13 Aug 2008, Christophe Dutang1 wrote:

> Hi,
>
> I'm currently R porting SF Mersenne Twister algorithm of Matsumoto and Saito. 
> To get the full power of their code, I want to use their fonction 
> fill_array32 which need aligned memory. That is to say I need to use the C 
> function memalign on windows, posix_memalign on linux and classic malloc on 
> Mac OS. In 'writing R extenstion', they recommand to use R_alloc function to 
> allocate memory in C.
>
> Does R_alloc return a pointer to aligned memory?
> if not how can I do this?
> probably no, because R crashes when I succesively R_alloc and fill_array32 
> (cf below) on my macbook with R 2.7.1.

R_alloc's alignment will be appropriate for holding any data type. It
will be offset from a value returned by malloc by a multiple of 8
bytes.

My recollection, which may be wrong, is that on both Intel and PPC
unaligned access to all basic data types is permitted but may be
inefficient (in particular on Intel), so the reason for your crash is
probably elsewhere.

Best,

luke

>
>
> Thanks in advance
>
> Kind regards
>
> Christophe
>
>
> PS : http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/howto-compile.html 
> provides an example of memalign.
>
> PPS : mac os report
>
> Thread 0 Crashed:
> 0   libSystem.B.dylib             	0x9341bb9e __kill + 10
> 1   libSystem.B.dylib             	0x93492ec2 raise + 26
> 2   libSystem.B.dylib             	0x934a247f abort + 73
> 3   randtoolbox.so                	0x15e65f1d 0x15e5d000 + 36637
> 4   randtoolbox.so                	0x15e614ef fill_array32 + 4038
> 5   randtoolbox.so                	0x15e6513d SFmersennetwister + 335
> 6   randtoolbox.so                	0x15e652c6 doSFMersenneTwister + 255
> 7   libR.dylib                    	0x00367a52 do_dotcall + 1394
> 8   libR.dylib                    	0x0038b5a2 Rf_eval + 1754
> 9   libR.dylib                    	0x0038f9a2 do_set + 592
> 10  libR.dylib                    	0x0038b366 Rf_eval + 1182
> 11  libR.dylib                    	0x0038b366 Rf_eval + 1182
> 12  libR.dylib                    	0x0038c140 do_begin + 58
> 13  libR.dylib                    	0x0038b366 Rf_eval + 1182
> 14  libR.dylib                    	0x0038b366 Rf_eval + 1182
> 15  libR.dylib                    	0x0038c140 do_begin + 58
> 16  libR.dylib                    	0x0038b366 Rf_eval + 1182
> 17  libR.dylib                    	0x0038d9a6 Rf_applyClosure + 663
> 18  libR.dylib                    	0x0038b25d Rf_eval + 917
> 19  org.R-project.R               	0x000189c3 run_REngineRmainloop + 569 
> (Rinit.m:442)
> 20  org.R-project.R               	0x0001142a -[REngine runREPL] + 260 
> (REngine.m:181)
> 21  org.R-project.R               	0x00002e91 main + 795 (main.m:126)
> 22  org.R-project.R               	0x00002b5a _start + 216
> 23  org.R-project.R               	0x00002a81 start + 41
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-help mailing list