R Package: Call C Function Within Rcpp - c

I am writing an R package that contains C and Rcpp. The goal is to call the C function from R and within Rcpp, eventually performing most of the analysis in Rcpp and only returning to R for minimal tasks. My package compiles and calling my function from R works fine.
#generate some matrix. Numeric is fine too. Must have column names, no row names
myMat <- matrix(data = 1:100, nrow = 10, ncol = 10,
dimnames = list(NULL, LETTERS[1:10]))
#This works. Put in full path, no expansion. It returns null to the console.
MinimalExample::WriteMat(mat = myMat, file = "Full_Path_Please/IWork.csv",
sep = "," ,eol = "\n", dec = ".", buffMB = 8L)
However, attempting the same thing in Rcpp produces a SIGSEV error. I think the problem is how I am passing arguments to the function, but I can't figure out the proper way.
#include <Rcpp.h>
using namespace Rcpp;
extern "C"{
#include "fwrite.h"
}
//' #export
// [[Rcpp::export]]
void WriteMatCpp(String& fileName, NumericMatrix& testMat){
Rcpp::Rcout<<"I did start!"<<std::endl;
String patchName = fileName;
int whichRow = 1;
std::string newString = std::string(3 - toString(whichRow).length(), '0')
+ toString(whichRow);
patchName.replace_last(".csv", newString+".csv");
//Set objects to pass to print function
String comma = ",";
String eol = "\n";
String dot = ".";
int buffMem = 8;
//This is where I crash, giving a SIGSEV error
fwriteMain(testMat, (SEXP)&patchName, (SEXP)&comma, (SEXP)&eol,
(SEXP)&dot, (SEXP)&buffMem);
}
Here is a link to the GitHub repository with the package. https://github.com/GilChrist19/MinimalExample

Your call from C++ to C is wrong. You can't just write (SEXP)& in front of an arbitrary data structure and hope for it to become a SEXP.
Fix
Use a line such as this to convert what you have in C++ to the SEXP your C function expects using Rcpp::wrap() on each argument:
//This is where I crash, giving a SIGSEV error
fwriteMain(wrap(testMat), wrap(patchName), wrap(comma),
wrap(eol), wrap(dot), wrap(buffMem));
Demo
edd#brad:/tmp/MinimalExample/MinEx(master)$ Rscript RunMe.R
I did start!
edd#brad:/tmp/MinimalExample/MinEx(master)$ cat /tmp/IDoNotWork.csv
A,B,C,D,E,F,G,H,I,J
1,11,21,31,41,51,61,71,81,91
2,12,22,32,42,52,62,72,82,92
3,13,23,33,43,53,63,73,83,93
4,14,24,34,44,54,64,74,84,94
5,15,25,35,45,55,65,75,85,95
6,16,26,36,46,56,66,76,86,96
7,17,27,37,47,57,67,77,87,97
8,18,28,38,48,58,68,78,88,98
9,19,29,39,49,59,69,79,89,99
10,20,30,40,50,60,70,80,90,100
edd#brad:/tmp/MinimalExample/MinEx(master)$
See https://github.com/GilChrist19/MinimalExample/tree/master/MinEx for a complete example.

Related

How to make a list with just the used inputs for a C module

I have a large module that uses a very large input buffer, consisting of many structures which, in turn, contain other structures and in the end each structure has several variables.
Out of these hundreds of input variables, my module (standalone C entity) uses only a fraction.
I would like to know if there is a way to make a list that will contain only the variables used in my module (would be perfect if it contains the variable type and links to structure/s that contains it).
I tried Doxygen (1.8.5) but I could generate a doc with all input variables, only.
[Later EDIT]
I add an example code and the desired outcome:
#include <stdio.h>
typedef struct subS1{
unsigned char bIn1;
unsigned char bIn2;
} subS1;
typedef struct S1{
struct subS1 stMySubStruct1;
struct subS1 stMySubStruct2;
struct subS1 stMySubStruct3;
} MyInputStruct_t;
void Foo1(MyInputStruct_t *Input);
void Foo2(MyInputStruct_t *Input);
MyInputStruct_t stMyInputStruct = {{1, 2}, {0, 0}, {9, 6}}; // large input buffer
int main() {
Foo1(&stMyInputStruct); // call to my Module 'main' function
return 0;
}
void Foo1(MyInputStruct_t *Input)
{
if(Input->stMySubStruct1.bIn1 == 1)
{
printf("bIn1 = %d\n", Input->stMySubStruct1.bIn1); // stMySubStruct1.bIn1 is used (read or write)
}
Foo2(Input);
return;
}
void Foo2(MyInputStruct_t *Input)
{
if(Input->stMySubStruct3.bIn2 == 0)
{
printf("bIn2 = %d\n", Input->stMySubStruct3.bIn2); // stMySubStruct3.bIn2 is used (read or write)
}
return;
}
The list with just the used inputs for Foo1(): e.g
stMyInputStruct.stMySubStruct1.bIn1 -> is used in Foo1()
stMyInputStruct.stMySubStruct1.bIn2 -> is NOT used
...
stMyInputStruct.stMySubStruct3.bIn2 -> is used in Foo2()
This is just a five-minute hack to demonstrate what I mean, so take it with a grain of salt and for what it is.
So first I downloaded pycparser from https://github.com/eliben/pycparser/
Then I edit the C-generator from https://github.com/eliben/pycparser/blob/master/pycparser/c_generator.py
... adding two lines to the constructor-code (adding two vars struct_refs + struct_ref):
class CGenerator(object):
""" Uses the same visitor pattern as c_ast.NodeVisitor, but modified to
return a value from each visit method, using string accumulation in
generic_visit.
"""
def __init__(self, reduce_parentheses=False):
""" Constructs C-code generator
reduce_parentheses:
if True, eliminates needless parentheses on binary operators
"""
# Statements start with indentation of self.indent_level spaces, using
# the _make_indent method.
self.indent_level = 0
self.reduce_parentheses = reduce_parentheses
# newly added variables here
self.struct_refs = set()
self.struct_ref = None
Then I edit two visitor-functions, to make them save information about the struct-references they parse:
def visit_ID(self, n):
if self.struct_ref:
self.struct_refs.add(self.struct_ref + "->" + n.name)
return n.name
def visit_StructRef(self, n):
sref = self._parenthesize_unless_simple(n.name)
self.struct_ref = sref
self.struct_refs.add(sref)
res = sref + n.type + self.visit(n.field)
self.struct_ref = None
return res
Running this modified piece of Python script over your example code, collects this information:
>>> cgen.struct_refs
{'Input',
'Input->stMySubStruct1',
'Input->stMySubStruct1->bIn1',
'Input->stMySubStruct3',
'Input->stMySubStruct3->bIn2'}
So with a bit more work, it should be able to do the job more generally.
This of course breaks apart in the face of memcpy, struct-member-access-through-pointers etc.
You can also try exploiting structure in your code as well. E.g. If you always call your input-struct "Input", things gets easier.

Call Racket function from C

I have a Racket module hw.rkt:
#lang racket/base
(provide hw)
(define (hw) (displayln "Hello, world!"))
I would like to write a C program that embeds the Racket runtime and applies the procedure (hw).
There is example code here which demonstrates how to embed the Racket runtime and apply a procedure that is in racket/base, or to read and evaluate an S-expression, but I've had no luck modifying this code to allow access to the (hw) procedure.
This page seems to say that it is possible to do what I want to do by first compiling hw.rkt to hw.c using raco ctool --c-mods, and this works just fine when I try it, but I still can't actually access the (hw) procedure.
If someone could post a complete example program, or simply describe which C functions to use, I would be very appreciative. From there I can figure out the rest.
Editing to provide examples of things I have tried.
I modified the example program to get rid of the "evaluate command line arguments" bit and skip straight to the REPL so that I could experiment. Thus (with "hw.c" the result of running raco ctool --c-mods hw.c ++libs racket/base hw.rkt):
#define MZ_PRECISE_GC
#include "scheme.h"
#include "hw.c"
static int run(Scheme_Env *e, int argc, char *argv[])
{
Scheme_Object *curout = NULL, *v = NULL, *a[2] = {NULL, NULL};
Scheme_Config *config = NULL;
int i;
mz_jmp_buf * volatile save = NULL, fresh;
MZ_GC_DECL_REG(8);
MZ_GC_VAR_IN_REG(0, e);
MZ_GC_VAR_IN_REG(1, curout);
MZ_GC_VAR_IN_REG(2, save);
MZ_GC_VAR_IN_REG(3, config);
MZ_GC_VAR_IN_REG(4, v);
MZ_GC_ARRAY_VAR_IN_REG(5, a, 2);
MZ_GC_REG();
declare_modules(e);
v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
config = scheme_current_config();
curout = scheme_get_param(config, MZCONFIG_OUTPUT_PORT);
save = scheme_current_thread->error_buf;
scheme_current_thread->error_buf = &fresh;
if (scheme_setjmp(scheme_error_buf)) {
scheme_current_thread->error_buf = save;
return -1; /* There was an error */
} else {
/* read-eval-print loop, uses initial Scheme_Env: */
a[0] = scheme_intern_symbol("racket/base");
a[1] = scheme_intern_symbol("read-eval-print-loop");
v = scheme_dynamic_require(2, a);
scheme_apply(v, 0, NULL);
scheme_current_thread->error_buf = save;
}
MZ_GC_UNREG();
return 0;
}
int main(int argc, char *argv[])
{
return scheme_main_setup(1, run, argc, argv);
}
Things that don't work (and their error messages):
Calling (hw) from the REPL
hw: undefined:
cannot reference undefined identifier
context...:
/usr/local/share/racket/collects/racket/private/misc.rkt:87:7
((dynamic-require 'hw 'hw))
standard-module-name-resolver: collection not found
for module path: hw
collection: "hw"
in collection directories:
context...:
show-collection-err
standard-module-name-resolver
/usr/local/share/racket/collects/racket/private/misc.rkt:87:7
((dynamic-require "hw.rkt" 'hw))
standard-module-name-resolver: collection not found
for module path: racket/base/lang/reader
collection: "racket/base/lang"
in collection directories:
context...:
show-collection-err
standard-module-name-resolver
standard-module-name-resolver
/usr/local/share/racket/collects/racket/private/misc.rkt:87:7
Editing the example code
v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("hw");
scheme_namespace_require(v);
Error:
standard-module-name-resolver: collection not found
for module path: hw
collection: "hw"
in collection directories:
context...:
show-collection-err
standard-module-name-resolver
SIGSEGV MAPERR sicode 1 fault on addr 0xd0
Aborted
(The segfault was probably because I didn't check the value of 'v' before trying to scheme_namespace_require it.)
Editing the example code mk. 2
v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("hw.rkt");
scheme_namespace_require(v);
Error:
hw.rkt: bad module path
in: hw.rkt
context...:
standard-module-name-resolver
SIGSEGV MAPERR sicode 1 fault on addr 0xd0
Aborted
(re: segfault: as above)
Editing the example code mk. 3
v = scheme_intern_symbol("racket/base");
scheme_namespace_require(v);
v = scheme_intern_symbol("./hw.rkt");
scheme_namespace_require(v);
(as above)
Editing the example code mk. 4
/* read-eval-print-loop, uses initial Scheme_Env: */
a[0] = scheme_intern_symbol("hw");
a[1] = scheme_intern_symbol("hw");
v = scheme_dynamic_require(2, a);
(as mk. 1, save the segfault)
Editing the example code mk. 5
/* read-eval-print loop, uses initial Scheme_Env: */
a[0] = scheme_intern_symbol("hw");
a[1] = scheme_eval(a[0], e);
scheme_apply(a[1], 0, NULL);
Error:
hw: undefined;
cannot reference undefined identifier
Answered by Matthew Flatt here. When using dynamic-require, I needed to quote the name of the module twice, not once. Thanks to Dr. Flatt for their assistance.

C program works from the command line but not from Python script?

Updated with whole python script.
Ok, so I am taking a Machine Learning course at Coursera.org and I want to see what some of these types of algorithmns can do with encryption techniques and test to see if it is possible to break an encryption say with a Neural Network. First I need to create a hash table for a training set, but I am having troubles with C string arrays, args, and Python passing strings as args to a C program.
Here is my lil C program called hashpipe
#include <stdio.h>
#define __USE_GNU
#include <crypt.h>
#include <string.h>
int main(int argc, char** argv){
char * salt = argv[2];
struct crypt_data data;
data.initialized = 0;
char * result = crypt_r(argv[1], id, &data);
printf("%s\n", result);
return 0 // This was the bugger!!! I forgot it!!!
}
And the Python script that calls this little program... Note that I am not sure if I use exit correctly.
#!/usr/bin/env python2.7
import sys, random, subprocess
pathToWL = str(sys.argv[1]) #Path to WordList
pathForHT = str(sys.argv[2]) #Path to create Hash Table; no, its not smokable
mId = str(sys.argv[3]) #id for use with crypt_r() see 'man 3 crypt_r'
SaltCharSet = str("a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9")
SaltCharSet = SaltCharSet.split(" ")
try:
fdWL = open(pathToWL, 'r')
except:
print "Could not open wordlist file."
exit()
try:
fdHT = open(pathForHT, 'a')
except:
print "Could not open file for hash table"
fdWL.close()
exit()
#We have our wordlist now step through the thing and start generating hashes.
toStop = False
cursor = 0 #Use the cursor later once this program evolves
while(not toStop):
try:
ln = str(fdWL.readline())
except:
toStop = True
continue
ln = ln.strip("\n")
if len(ln) < 6:
continue
# create random salts
# send ln, id, and salts to hashpipe
salt = []
lenOfSalt = random.randint(6,16)
while(len(salt) < lenOfSalt + 1):
aORn = random.randint(0,1)
if aORn == 0:# Its a letter
uORl = random.randint(0,1)
if uORl == 0:
salt.append(SaltCharSet[(random.randint(0,25))].upper())
elif uORl == 1:
salt.append(SaltCharSet[(random.randint(0,25))].lower())
else:
print "Random Int 'uORl' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
exit() # I don't know what happened
break #in case exit() fails or is used incorrectly
elif aORn == 1:# Its a number
salt.append(SaltCharSet[(random.randint(26, 35))])
else:
print "Random Int 'aORn' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
exit() # I don't know what happened
break #in case exit() fails or is used incorrectly
#Generated Salt
salt = "".join(salt)
wholeArg2 = str("$"+mId+"$"+salt)
try:
mHash = str(subprocess.check_output(["hashpipe", ln, wholeArg2]))
except:
print " error getting hash"
#Clean-up
fdHT.close()
fdWL.close()
toStop = True
exit()
break
#Generated hash, now write it to the fdHT file
print str(ln+" "+wholeArg2+"\t"+mHash)
fdHT.write(str(ln+"\t"+mHash+"\n"))
cursor = fdWL.tell()
fdHT.close()
fdWL.close()
return 0 #Yes, I forgot it here too, probably why my script never ended! LOL!!! so simple!!!
I have been changing this part heavily and nothing works, whats the deal? It works on the command line but not from python. For instance the strip('\0') and str(r"$") are some modification I recently made but still didn't work. Maybe I'll just write a bash script instead...
Note, that the compiled C program does what it should on the cmd-line.
DOH!
I needed to return 0 in my C program. Its working now just to let you know. Creating my first hash table of sorts :)

passing unevaluated expressions to C/C++

I'd like to pass a variable number of arguments from a function to C/C++, but would like to leave the arguments unevaluated and at the same time don't want to do any computations in R (aside from calling the C/C++ function), i.e. I don't want to call substitute in my R function. One option for this that I thought I could use is .External and doing smth like this:
R_fn = function(...) .External("cpp_fn", ...)
...
# and in C code:
SEXP cpp_fn (SEXP arglist) {
}
However .External is evaluating arguments in ..., so if I try something like
rm(x, y) # just making sure these don't exist
R_fn(x*y)
I get an error because R is trying to evaluate x*y before sending it to the function.
To contrast, the following works in R:
f = function(...) g(...)
g = function(x, ...) print(substitute(x))
f(x*y*z)
# x * y * z
What other options do I have? Clearly it's possible to do as R itself does it for a number of functions, e.g. substitute itself, but I don't understand how to do it. I added the rcpp tag because my eventual usage of this is going to be in Rcpp.
One possibility is to do what match.call does (thanks to Ricardo Saporta for pointing me in that direction). This requires copy-pasting a few definitions from R source code that I won't do here, but the basic idea is to get the calling function from R_GlobalContext and then extract the function arguments from there. The rough sketch is as follows:
R_fn = function(...) .Call("cpp_fn")
// and in C++ code
Language cpp_fn() {
SEXP sysp = ((RCNTXT*)R_GlobalContext)->sysparent;
RCNTXT *cptr = (RCNTXT*)R_GlobalContext;
while (cptr != NULL) {
if (cptr->callflag & CTXT_FUNCTION && cptr->cloenv == sysp)
break;
cptr = cptr->nextcontext;
}
cptr = cptr->nextcontext; // because this is called from .Call and not from R_fn
// and now cptr->promargs has the unevaluated arguments to do as one pleases
// e.g.
Language firstArg(R_PromiseExpr(CAR(cptr->promargs)));
return firstArg;
}

How to save a struct to a file using a variable filename in Octave?

In Octave I would like to save a struct to a textfile where the name of the file is decided during the runtime of the script. With my approach I always get an error:
expecting all arguments to be strings.
(For a fixed filename this works fine.) So how to save a struct to a file using a variable filename?
clear all;
myStruct(1).resultA = 1;
myStruct(1).resultB = 2;
myStruct(2).resultA = 3;
myStruct(2).resultB = 4;
variableFilename = strftime ("result_%Y-%m-%d_%H-%M.mat", localtime(time()))
save fixedFilename.mat myStruct;
% this works and saves the struct in fixedFilename.mat
save( "-text", variableFilename, myStruct);
% this gives error: expecting all arguments to be strings
In Octave, When using save as a function you need to do something like this:
myfilename = "stuff.txt";
mystruct = [ 1 2; 3 4]
save("-text", myfilename, "mystruct");
The above code will create a stuff.txt file and the matrix data is put in there.
The above code will only work when mystruct is a matrix, if you have a cell of strings, it will fail. For those, you can roll your own:
xKey = cell(2, 1);
xKey{1} = "Make me a sandwich...";
xKey{2} = "OUT OF BABIES!";
outfile = fopen("something.txt", "a");
for i=1:rows(xKey),
fprintf(outfile, "%s\n", xKey{i,1});
end
fflush(outfile);
fclose(outfile);

Resources