Interface between R and C libraries / BLAS / OS X - c

I'm writing some C programs (I'm quite a beginner in C), that I compile and call from R. In order to be as efficient as possible, I try to use as much as possible functions from Lapack and BLAS (which contain highly optimised routine to perform basic operations).
I compile my .c codes thanks to, classically:
system ('R CMD SHLIB blablabla.c')
Here is the "preamble" of my average .c code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <R_ext/blas.h>
#include <R_ext/Lapack.h>
Here is my "dummy" problem: I need to compute the "argmin of a real vector", i.e., the index of the minimal component of the vector (in practice, for huge vectors and a lot of times, so that I really like to use an optimised routine, and not a basic "for" loop).
"Unfortunately", in "R_ext/blas.h", the function IDMIN does not appear ("R_ext/blas.h" only contains the function IDAMAX, that returns the index of the maximum absolute value component).
In R, such a task is easily achieved by "which.min()" (which is, I guess, based on an optimised code), so I guess that it should "somewhere" exists the C code (certainly based on a FORTRAN code) that achieves this task, and this is exactly what I need...
In other words: what library should I add to my ".c preamble" and what C function should I use to be able to use an "optimised which.min()" in my C code (that I compile and call from R) ?
Another question: On Mac OS X, how to add some C libraries to the existing C libraries of R (still, with the view to compile and use C from R, which is in my view very convenient) ?
Thanks.

Related

Program memory not increasing when including math.h library

I'm a bit of a newbie to the Atmel world. Once Upon a time I could write and compile C with Visual Studio but am a bit out of practice.
So I'm trying to get an understanding of memory usage in a microcontroller ATTINY1616. I opened Atmel studios, created a C executable project and chose the correct microcontroller. I build the project which has next to nothing in it and see that the program memory is 154 bytes. This is my baseline.
Now I tried to add the line #include <math.h> to see if my program memory usage would increase. It didn't. Then I tried adding float a = 2.000678f; inside the main. Still no increases after building the project. What am I misunderstanding here?
/*
* GccApplication2.c
*
* Created: 12/20/2018 9:21:43 PM
* Author : joely
*/
#include <avr/io.h>
#include <math.h>
int main(void)
{
float a = 2.000678f;
/* Replace with your application code */
while (1)
{
}
}
Header files typically only contain declarations of functions, not the definitions.
You're not using any of the functions declared in math.h, so the library they reside in isn't getting linked into your program. If you use one of them, for example float b = sin(a), then the contents of the math library is required and is linked in (assuming you pass -lm to gcc to do so).
So after asking some friends with microcontroller experience I found the solution.
In Atmel Studio you need to go to Project-->Application Properties--->Toolchain and Change optimization to none.
Then it recognizes my floats and stores them in program memory, and when atan() is used to perform a calc it also fills up the space with the same above code.

How to call dpois_raw C stats routine from R

I'm trying to figure out a way to call stats package's "dpois_raw" routine rather than the "dpois" wrapper using .Call .External or whatever means.
"dpois_raw" is not listed in the package environment (stats:::C_*) nor when I do getDLLRegisteredRoutines("stats") so I am probably out of luck, but I wonder whether someone R/C expert knows of a workaround.
The dpois_raw routine is provided by the Rmath.h header, and it doesn't appear to actually be exposed as part of the stats package (or otherwise); however, it is made available to C / C++ code through the Rmath.h header.
The simplest way to expose it would be with your own C / C++ code exposing that code, e.g. (code stub)
#include <R.h>
#include <Rmath.h>
SEXP my_dpois_raw(<...>) {
double result = dpois_raw(<...>);
return result;
}
This routine would then be callable from R with something like
.Call("my_dpois_raw", <...>)
See Hadley's r-pkgs section on using compiled code in R packages for some more information on including C / C++ code in an R package.

Including a personal.h library in C code callable from R

I've been struggling to find proper information on the web to solve this problem, in case it is an easy task please guide me through.
My final goal is to write some R functions that call C subroutines with the .Call function. In general there are no problems in doing this when R.h and Rinternals.h are sufficient.
My problem is: I would need to use in the C code some functions that are in a "personal.h" C library. I already compiled this library with gcc, but if I just try to add
#include "personal.h"
at the beginning after
#include <R.h>
#include <Rinternals.h>
like I would do if it was a standalone C file, when I then call any function from that package in the code, while compiling with R CMD SHLIB I get an error message telling me that it was not possible to find that function.
What should I do in order to include a C library in a C routine callable from R?
Read the "Writing R Extensions" manual which came with your copy of R.
Here, you need PKG_CPPFLAGS to tell R about your include files / headers. Later, you will need to tell it about your library.
Look at other small packages using C code as eg my digest package. And yes, there are in fact numerous tutorials on the Web for this too.

what is the advantage of including .c files?

when I read ucos source files, I find this code in ucos_ii.c
#include "os_core.c"
#include "os_mbox.c"
#include "os_mem.c"
#include "os_q.c"
#include "os_sem.c"
#include "os_task.c"
#include "os_time.c"
what's the advandage of including .c files?
By doing this, they may be allowing the compiler to do more inlining and/or space optimization. uCos is an embedded operating system, so anything that saves space or time is a good thing. (Within reason, of course)
It can simplify the building process by requiring a simpler makefile. In this case, 7 less files need to be added to the makefile. However, as projects become large, it quickly becomes unwieldy.
Another downside is any variable which would normally have internal linkage is now available to the other c files.
I hope someone can correct me if I'm wrong, since my episodes of coding in C are far and few in between, but AFAIK, adding a .c file like that lets you treat all the functions and whatnot that are defined in that file as if they were coded directly in the file they are included in. That should let you build up a more complex file from simpler, more re-usable parts.
i think this is used to import the system library function,and when you need to use a method that from system library than it works

What modern C compiler can I use to build this 1992 MS-DOS program?

I was given the source code to modify an MS-DOS program built back in 1992. I have the EXE file and it runs fine, but I need to modify the source code. The source code needs the below headers to compile.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <dos.h>
#include <dir.h>
#include <alloc.h>
#include <ctype.h>
#include <string.h>
#include <mem.h>
#include <values.h>
Does anyone know what was used and are there any modern compilers that can handle this? I tried with Visual Studio 2010 and GCC "out of the box", but it fails because some headers are missing (dir.h, alloc.h, mem.h, values.h)
It might be more interesting to ask what what function declarations, type declarations, global variable declarations and macros it needs to have. The particular arrangement of those things into headers isn't very interesting as long as they are all there.
So comment out the offending #includes and let the compiler complain about the bits it is missing. Then you know what you're looking for.
You could try the Open Watcom compiler, which is one of the few relatively up-to-date compilers that builds 16-bit DOS executables. Other than finding an old MS or Borland compiler (or whatever was originally used), that's probably the easiest route.
If you want to rebuild for a different platform instead of rebuilding for DOS again, you'll likely have to make a lot of changes to the program itself. That may be worthwhile, but may be a lot of work and have a lot of surprise headaches.
There's Turbo C++ 1.01, not so modern, though, that appears to have all these header files as well. I still occasionally use it.
You might try using DJGPP. According to the documentation, it may have the headers you need.
a) Remove all the header files
b) Try a compile
c) Look up which header file the undefined function/type is int
d) Add the header file
e) repeat

Resources