How can I call a rust function from R that returns Vec<Vec<f64>>? - r

I have implemented a function for sampling from M different normal distributions N times in Rust because my R code was too slow. It is also parallelized. Here is the pure Rust code:
use rand_distr::{Normal, Distribution};
use rayon::prelude::*;
fn rust_rprednorm(n: i32, means: Vec<f64>, sds: Vec<f64>) -> Vec<Vec<f64>> {
let mut preds = vec![vec![0.0; n as usize]; means.len()];
preds.par_iter_mut().enumerate().for_each(|(i, e)| {
let mut rng = rand::thread_rng();
(0..n).into_iter().for_each(|j| {
let normal = Normal::new(means[i], sds[i]).unwrap();
e[j as usize] = normal.sample(&mut rng);
})
});
preds
}
Which I am trying to call from R using the rextendr library. The code is inside a utils.R file I import using source():
code <- r"(
use rand_distr::{Normal, Distribution};
use rayon::prelude::*;
#[extendr]
fn rust_rprednorm(n: i32, means: Vec<f64>, sds: Vec<f64>) -> Vec<Vec<f64>> {
let mut preds = vec![vec![0.0; n as usize]; means.len()];
preds.par_iter_mut().enumerate().for_each(|(i, e)| {
let mut rng = rand::thread_rng();
(0..n).into_iter().for_each(|j| {
let normal = Normal::new(means[i], sds[i]).unwrap();
e[j as usize] = normal.sample(&mut rng);
})
});
preds
}
)"
rust_source(code = code, dependencies = list(`rand` = "0.8.5", `rand_distr` ="0.4.3", `rayon` = "1.6.1"))
The error is:
Error in `invoke_cargo()`:
! Rust code could not be compiled successfully. Aborting.
✖ error[E0277]: the trait bound `Robj: From<Vec<Vec<f64>>>` is not satisfied
--> src\lib.rs:6:5
|
6 | #[extendr]
| ^^^^^^^^^^ the trait `From<Vec<Vec<f64>>>` is not implemented for `Robj`
|
= help: the following other types implement trait `From<T>`:
<Robj as From<&'a [T]>>
<Robj as From<&Altrep>>
<Robj as From<&Primitive>>
<Robj as From<&Robj>>
<Robj as From<&Vec<T>>>
<Robj as From<&extendr_api::Complexes>>
<Robj as From<&extendr_api::Doubles>>
<Robj as From<&extendr_api::Environment>>
and 71 others
= note: this error originates in the attribute macro `extendr` (in Nightly builds, run with -Z macro-backtrace for more info)
✖ error: aborting due to previous error
Traceback:
1. source("inla_predictive_distribution_utils.R")
2. withVisible(eval(ei, envir))
3. eval(ei, envir)
4. eval(ei, envir)
5. rust_source(code = code, dependencies = list(rand = "0.8.5",
. rand_distr = "0.4.3", rayon = "1.6.1"))
6. invoke_cargo(toolchain = toolchain, specific_target = specific_target,
. dir = dir, profile = profile, quiet = quiet, use_rtools = use_rtools)
7. check_cargo_output(compilation_result, message_buffer, tty_has_colors(),
. quiet)
8. ui_throw("Rust code could not be compiled successfully. Aborting.",
. error_messages, call = call, glue_open = "{<{", glue_close = "}>}")
9. withr::with_options(list(warning.length = message_limit_bytes),
. rlang::abort(message, class = "rextendr_error", call = call))
10. force(code)
11. rlang::abort(message, class = "rextendr_error", call = call)
12. signal_abort(cnd, .file)
I figured a nested vector would be supported since a regular vector is, but it appears not, do I need to implement this trait for Robj, or is there another way to go about it? I am also not sure if this is the recommended way to call Rust code from R.

Related

Map.change is not defined

I'm trying to invoke the Map.change function. It's clearly defined in the documentation but it just doesn't work for me. I've tried opening different namespaces (such as FSharp.Core, Microsoft.FSharp.Collections), with no success.
open FSharp.Collections
[<EntryPoint>]
let main argv =
let testMap = Map.empty.Add("test", true).Add("otherTest", false)
let newTestMap =
testMap
|> Map.change // Here's the error!
"otherTest"
(fun v ->
match v with
| Some b -> Some (not b)
| None -> None )
printf(newTestMap)
0
I'm getting the following error when trying to run the sample code above:
error FS0039: The value, constructor, namespace or type 'change' is not defined.
How do I use Map.change?
Your code works (apart from the printf). Maybe check your F# or .NET (Core) version. The Collections namespace doesn't have to be open.
let testMap = Map.empty.Add("test", true).Add("otherTest", false)
let newTestMap =
testMap
|> Map.change // Here's the error!
"otherTest"
(fun v ->
match v with
| Some b -> Some (not b)
| None -> None )
printf "%A" newTestMap // map [("otherTest", true); ("test", true)]val it : unit = ()

Rust ndarray type error: trait bound not implemented

I'm using ndarray and ndarray_einsum_beta to mimic numpy.einsum.
I seem to be getting a type error trying to implement the basic example given:
let m1 = arr1(&[1, 2]);
let m2 = arr2(&[[1, 2], [3, 4]]);
println!("{:?}", einsum("i,ij->j", &[&m1, &m2]));
For both m1 and m2 I get the error:
the trait bound ndarray::ArrayBase<ndarray::OwnedRepr<{integer}>,
ndarray::Dim<[usize; 1]>>: ndarray_einsum_beta::ArrayLike<_> is not
satisfied
the trait ndarray_einsum_beta::ArrayLike<_> is not implemented for
ndarray::ArrayBase<ndarray::OwnedRepr<{integer}>,
ndarray::Dim<[usize; 1]>>
note: required for the cast to the object type dyn
ndarray_einsum_beta::ArrayLike<_>rustc(E0277)
As shown in the console on compilcation:
Whole main.rs:
use ndarray::prelude::*;
use ndarray_einsum_beta::*;
fn main() {
println!("Hello, world!");
let m1 = arr1(&[1, 2]);
let m2 = arr2(&[[1, 2], [3, 4]]);
println!("{:?}", einsum("i,ij->j", &[&m1, &m2]));
}
Whole Cargo.toml:
[package]
name = "clean_slate"
version = "0.1.0"
authors = ["Jonathan <jonathanwoollettlight#gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ndarray = "0.13.0"
ndarray_einsum_beta = "0.4.4"
I really cannot figure what I have done wrong here, my best guess right now is perhaps a bad version combination of ndarray and ndarray_eisum_beta.
Any help would be greatly appreciated.
Apparently ndarray_einsum_beta depends on ndarray-0.12. Changing the dependencies of your project solves the problem:
[dependencies]
ndarray = "0.12"
ndarray_einsum_beta = "0.4.4"

LLVMLITE Hello World Example Produces Wrong Output

The problem I am faced with regards to llvmlite is producing a simple hello world example.
I am unable to display the string global variable value in the function I created.
It appears to always print out the number one.
I have already tried to return the stringtype which produced a error.
from llvmlite import ir
i64 = ir.IntType(64)
i8 = ir.IntType(16)
hellostr = 'hello, world!'
stringtype = ir.ArrayType(i64, len(hellostr))
module = ir.Module( name="m_hello_example" )
hello = ir.GlobalVariable(module, stringtype, '.str4')
fn_int_to_int_type = ir.FunctionType(i64, [stringtype.as_pointer()] )
fn_hel = ir.Function( module, fn_int_to_int_type, name="fn_hel" )
fn_hel_block = fn_hel.append_basic_block( name="fn_hel_entry" )
builder = ir.IRBuilder(fn_hel_block )
# zero = builder.constant(i64, 0)
# const_1 = ir.Constant(stringtype,1);
# builder.ret(const_1)
const_1 = ir.Constant(i64,1);
# print(const_1)
builder.ret(const_1)
print( module )
I was expecting the output to print out the string 'hello, world!'.
Any help would be much appreciated.
Thanks.
It ended up that I was able to solve my problem with the following code:
import llvmlite.ir as ir
import llvmlite.binding as llvm
from ctypes import CFUNCTYPE
def main():
m = ir.Module()
func_ty = ir.FunctionType(ir.VoidType(), []) #defining printer function as type void
func = ir.Function(m, func_ty, name="printer") #define function as printer
builder = ir.IRBuilder(func.append_basic_block('entry')) #defining the entry point of the function printer
fmt = "%s\n\0" #in function printf allows for inserting arg in, next global_fmt statements allow for creating #"fstr" assignment
c_fmt = ir.Constant(ir.ArrayType(ir.IntType(8), len(fmt)),
bytearray(fmt.encode("utf8")))
global_fmt = ir.GlobalVariable(m, c_fmt.type, name="fstr")
global_fmt.linkage = 'internal'
global_fmt.global_constant = True
global_fmt.initializer = c_fmt
arg = "Hello, World!\0" #args will be passed into printf function.
c_str_val = ir.Constant(ir.ArrayType(ir.IntType(8), len(arg)),
bytearray(arg.encode("utf8"))) #creates the c_str_value as a constant
printf_ty = ir.FunctionType(ir.IntType(32), [], var_arg=True) #creation of the printf function begins here and specifies the passing of a argument
printf = ir.Function(m, printf_ty, name="printf")
c_str = builder.alloca(c_str_val.type) #creation of the allocation of the %".2" variable
builder.store(c_str_val, c_str) #store as defined on the next line below %".2"
voidptr_ty = ir.IntType(8).as_pointer()
fmt_arg = builder.bitcast(global_fmt, voidptr_ty) #creates the %".4" variable with the point pointing to the fstr
builder.call(printf, [fmt_arg, c_str]) #We are calling the prinf function with the fmt and arg and returning the value as defiend on the next line
builder.ret_void()
#Next lines are for calling llvm and returning the assembly.
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
print(str(m)) #PRINTING OUT THE ASSEMBLY
llvm_module = llvm.parse_assembly(str(m)) #Parsing teh assembly
tm = llvm.Target.from_default_triple().create_target_machine() #creating the target machine
with llvm.create_mcjit_compiler(llvm_module, tm) as ee:
ee.finalize_object() #Making sure all modules owned by the execution engine are fully processed and usable for execution
fptr = ee.get_function_address("printer") #fptr will reference the printer function
py_func = CFUNCTYPE(None)(fptr)
py_func() #run the function printer
if __name__ == "__main__":
main()
It appears that I didn't correctly assign the variable and hence why I wasn't returning anything.

F# Type Provider Referencing Custom Types

I'm constructing a simple type provider, but I seem to be running into problems when referencing types I created. For instance, given
namespace Adder
type Summation = Summation of int
module QuickAdd =
let add x y = x + y |> Summation
I want to make the following test case pass:
module Adder.Tests
open Adder
open NUnit.Framework
type Simple = QuickAddProvider<1, 2>
[<Test>]
let ``Simple sample is 3`` () =
let foo = Simple()
Assert.AreEqual(foo.Sample, Summation 3)
With the following type provider:
namespace Adder
open Microsoft.FSharp.Core.CompilerServices
open ProviderImplementation.ProvidedTypes
open System.Reflection
[<TypeProvider>]
type public QuickAddProvider (config : TypeProviderConfig) as this =
inherit TypeProviderForNamespaces ()
let ns = "Adder"
let asm = Assembly.GetExecutingAssembly()
let paraProvTy = ProvidedTypeDefinition(asm, ns, "QuickAddProvider", Some typeof<obj>)
let buildTypes (typeName:string) (args:obj[]) =
let num1 = args.[0] :?> int
let num2 = args.[1] :?> int
let tpType = ProvidedTypeDefinition(asm, ns, typeName, Some typeof<obj>)
let result = QuickAdd.add num1 num2
let orig = ProvidedProperty("Sample", typeof<Summation>, GetterCode = (fun args -> <## result ##>))
tpType.AddMember(orig)
tpType.AddMember(ProvidedConstructor([], InvokeCode = (fun args -> <## () ##>)))
tpType
let parameters =
[ProvidedStaticParameter("Num1", typeof<int>)
ProvidedStaticParameter("Num2", typeof<int>)]
do paraProvTy.DefineStaticParameters(parameters, buildTypes)
do this.AddNamespace(ns, [paraProvTy])
[<TypeProviderAssembly>]
do()
I run into unexpected errors in the test file:
The type provider 'Adder.QuickAddProvider' reported an error in the context of provided type 'Adder.QuickAddProvider,Num1="1",Num2="2"', member 'get_Sample'. The error: Unsupported constant type 'Adder.Summation'
With the following errors in the generated file:
The type "Summation" is not defined
The namespace or module "Adder" is not defined
The test case compiles and passes when replacing the Summation type with int, so I know my type provider isn't terribly wrong. Do I need to somehow "import" the Summation type somewhere?
This error usually means that you are creating a quotation that contains a value of custom type. The quotations in type providers can only contain values of primitive types - the compiler knows how to serialize these - but it cannot handle custom types.
In the snippet, this happens here:
let result = QuickAdd.add num1 num2
let orig = ProvidedProperty("Sample", typeof<Summation>, GetterCode = (fun args ->
<## result ##>))
Here, the GetterCode returns a quotation containing value of type Summation which is not supported. To make this work, you can do various things - generally, you'll need to come up with some other quoted expression that produces the value you want.
One option is to do the calculation inside the quotation rather than outside:
<## QuickAdd.add num1 num2 ##>
The other option would be to re-create the Summation value in the quotation:
let (Summation n) = result
<## Summation n ##>
This works, because it only needs to serialize a primitive int value and then generate a call to the Summation case constructor.

Mutable Data in OCaml

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error,
Here is my code
type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;
let max_seq_length = ref 200;;
exception Out_of_bounds;;
exception Vec_store_full;;
let vec_mag {a=c;b=d} = sqrt( c**2.0 +. d**2.0);;
let make_vec_store() =
let vecarr = ref ((Array.create (!max_seq_length)) {a=0.0;b=0.0}) in
{seq= !vecarr;size=0};;
When I do this in ocaml top-level
let x = make _ vec _store;;
and then try to do x.size I get this error
Error: This expression has type unit -> vec_store
but an expression was expected of type vec_store
Whats seems to be the problem? I cant see why this would not work.
Thanks,
Faisal
make_vec_store is a function. When you say let x = make_vec_store, you are setting x to be that function, just like if you'd written let x = 1, that would make x the number 1. What you want is the result of calling that function. According to make_vec_store's definition, it takes () (also known as "unit") as an argument, so you would write let x = make_vec_store ().
try x = make_ vec_store()
As a follow up to the excellent answere provided. You can tell that your example line:
# let x = make_vec_store;;
val x : unit -> vec_store = <fun>
returns a function as the repl will tell you this. You can see from the output that x is of type <fun> that takes no parameters unit and returns a type vec_store.
Contrast this to the declaration
# let x = 1;;
val x : int = 1
which tells you that x is of type int and value 1.

Resources