What are the restrictions on thread safety in RcppParallel? - r

I am sorry this question is hopelessly vague, but I have a MWE that does not work as I hoped it would (distilled down from a more complex problem I actually wanted to solve, so don't pooh-pooh it as being a toy.). First the Rcpp code:
// accumulate # of non-na elements seen on the input iterator.
void accum_nonna(RVector<double>::const_iterator vecit,
RVector<double>::const_iterator end,
RVector<int>::iterator xret) {
for (;vecit != end;++vecit) {
if (! NumericVector::is_na(*vecit)) { *xret += 1; }
}
}
struct MWE : public Worker
{
// source vector
const RVector<double> input;
// accumulated value
RVector<int> output;
// constructors
MWE(const NumericVector input, IntegerVector output) : input(input), output(output) {}
MWE(const MWE& mwes, Split) : input(mwes.input), output(mwes.output) {}
// accumulate just the element of the range I have been asked to
void operator()(std::size_t begin, std::size_t end) {
accum_nonna(input.begin() + begin, input.begin() + end, output.begin());
}
// join my value with that of another MWE
void join(const MWE& rhs) {
output[0] += rhs.output[0];
}
};
// [[Rcpp::export]]
IntegerVector dumbCount(NumericVector x) {
// allocate the output
IntegerVector output(1);
// declare the instance
MWE mwes(x,output);
// call parallel_reduce to start the work
parallelReduce(0, x.length(), mwes, 5000);
// return the computed number of non-na elements
return output;
}
As far as the user can see, you get a silly function in R called dumbCount that counts the number of non-NA elements in the input vector. It seems to work fine when the input is under the grain size of 5000, but does not work when over it:
> source('mwe.cpp')
> dumbCount(rnorm(4995))
[1] 4995
> dumbCount(rnorm(5005))
[1] 7112
Clearly I have done something that is not threadsafe.
I would suspect the join operation, but I noticed that when I change the code in operator() to output[0] = end - begin; (to solve a different toy problem) I get an entirely different failure mode in that the join does not seem to actually work, rather dumbCount(rnorm(5001)) returns 2501 fairly consistently.

Related

Check for abort

I used to depend on the package RcppProgress to check for user abortion inside a long loop with Progress::check_abort(). But I just received an email from the CRAN team to tell me (and to other maintainers) that RcppProgress has bugs and will be removed soon in absence of maintainer (actually it seems already removed). Is there another way to check for abortion?
I found that R_CheckUserInterrupt exists. How to change my code to use this function? In Writing R extensions the function returns void so I do not understand how it works. It seems to exit immediately.
Rcpp::checkUserInterrupt seems to present the same behavior. And R: How to write interruptible C++ function, and recover partial results presents a kind of hack not recomented by its author. I would like to exit the loop correctly cleaning object allocated on the heap and returning partial output
// [[Rcpp::depends(RcppProgress)]]
#include <progress.hpp>
#include <Rcpp.h>
// [[Rcpp::export]]
SEXP f()
{
for( int i = 0 ; i < 100000 ; i++)
{
if (Progress::check_abort())
{
delete some_var;
return partial_output;
}
else
//do_stuff();
}
}
After reading the sources of Rcpp I found that Rcpp::checkUserInterrupt() throw an internal::InterruptedException. This works:
for (long i = 0 ; i < 100000000 ; i++)
{
try
{
Rcpp::checkUserInterrupt();
}
catch(Rcpp::internal::InterruptedException e)
{
delete some_var;
return partial_output;
}
}
It is slow but exactly like Process::check_abort. Optionally, as advised in Rcpp Attributes, one can check only every 100 or 1000 iteration to speed up the code.
for (long i = 0 ; i < 100000000 ; i++)
{
if (i % 100 == 0)
{
try
{
Rcpp::checkUserInterrupt();
}
catch(Rcpp::internal::InterruptedException e)
{
delete some_var;
return partial_output;
}
}
}

Coin Change Dynamic Programming

QUESTION:
I'm having trouble finding the minimum amount of coins needed to reach a specific sum. I'm pretty sure this is done easiest recursively and using the dynamic programming methodology, I should basically get Math.min("takeACoin","leaveACoin");
Unfortunately, My code doesn't terminate though I do have if statements that terminate under the condition that the sum is met, the array of coins is depleted, or if the sum is over. Please look at my code below and let me know what I'm doing wrong and especially why my code continues executing until it receives a stackoverflow error though I have the appropriate terminating conditions.
CODE:
private static final int S = 3;
public static int arr[] = {1,2};
public static void main(String[] args) {
Interview i = new Interview();
i.sumCoins(arr, 0);
}
public int sumCoins(int[] ar, int sum) {
//if the sum is met, dont add any coins, just return 0
if(sum == S){
return 0;
}
//if the sum is greater, then return max value as it is impossible to get less sum
if(sum > S){
return Integer.MAX_VALUE;
}
//if the array is out of coins return max value
if(ar.length == 0){
return Integer.MAX_VALUE;
}
//if the sum is less than S and there is still more coins to use, keep checking
//add the first coin
int tmpSum = sum + ar[0];
//delete the first coin from the list
int[] tmp = Arrays.copyOfRange(ar, 1, ar.length);
//add one coin to the solution
int one = 1+sumCoins(tmp, tmpSum);
//don't add one coin to the solution
int two = sumCoins(ar,sum);
//see which is more minimized
return Math.min(one,two);
}
Requested Stack Trace:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Math.min(Math.java:879)
at java.util.Arrays.copyOfRange(Arrays.java:2623)
at Interview.sumCoins(Interview.java:28)
at Interview.sumCoins(Interview.java:32)
at Interview.sumCoins(Interview.java:32)
The answer to this question is in regards to how I was implementing my dynamic programming. I was using the original array in the case where you left the coin. this is incorrect. In more detail:
If you take the coin: get rid of the first (coin) index of the array, add the sum, add +1 for the number of coins.
If you don't take the coin: get rid of the first (coin) index of the array since you're leaving that coin to not be considered.
In my solution, I received a stackoverflow because I was going through the "leaving the coin" scenario infinite times as the array never decreased and I wasn't actually "leaving the coin".
Correct Code here:
private static final int S = 5;
public static int arr[] = {1,1,1,1,1};
public static void main(String[] args) {
Interview i = new Interview();
System.out.println(i.sumCoins(arr, 0));
}
public int sumCoins(int[] ar, int sum) {
//if the sum is met, dont add any coins, just return 0
if(sum == S){
return 0;
}
//if the sum is greater, then return global array (not local)
//length +1 as it's impossible to get more coins than indices
if(sum > S){
return arr.length+1;
}
//if the array is out of coins return max value
if(ar.length == 0){
return arr.length+1;
}
//if the sum is less than S and there is still more coins to use, keep checking
//add the first coin
int tmpSum = sum + ar[0];
//delete the first coin from the list
int[] tmp = Arrays.copyOfRange(ar, 1, ar.length);
//add one coin to the solution
int one = 1+sumCoins(tmp, tmpSum);
//don't add one coin to the solution
int two = sumCoins(tmp,sum);
//see which is more minimized
return Math.min(one,two);
}

How to bind an argument with a dual function?

Consider these C functions:
#define INDICATE_SPECIAL_CASE -1
void prepare (long *length_or_indicator);
void execute ();
The prepare function is used to store a pointer to a delayed long * output variable.
It can be used in C like this:
int main (void) {
long length_or_indicator;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
long length = lengh_or_indicator;
// do something to handle the normal case which has a length
}
}
I am trying to achieve something like this in Vala:
int main (void) {
long length;
long indicator;
prepare (out length, out indicator);
execute ();
if (indicator == INDICATE_SPECIAL_CASE) {
// do something to handle special case
}
else {
// do something to handle the normal case which has a length
}
}
How to write the binding for prepare () and INDICATE_SPECIAL_CASE in Vala?
Is it possible to split the variable into two?
Is it possible to avoid using pointers even though the out variable is written to after the call to prepare () (in execute ())?
The problem with using out is that Vala is going to generate lots of temporary variables along the way, which will make the reference wrong. What you probably want to do is create a method in your VAPI that hides all this:
[CCode(cname = "prepare")]
private void _prepare (long *length_or_indicator);
[CCode(cname = "execute")]
private void _execute ();
[CCode(cname = "prepare_and_exec")]
public bool execute(out long length) {
long length_or_indicator = 0;
prepare (&length_or_indicator);
execute ();
if (length_or_indicator == INDICATE_SPECIAL_CASE) {
length = 0;
return false;
} else {
length = lengh_or_indicator;
return true;
}
}

Recursion not working properly

This is a recursive solver to try to solve Euler#60. http://projecteuler.net/problem=60
The solver runs through, but fails to find a solution for the last array member, so backtracks (like I think it's supposed to) but when I get back to the first array member, the loop runs out all the way. Can anybody spot for me why it doesn't stop at the next prime?
I've posted just the solver function below; the other function (Concat check) works properly and returns true for a partially filled array.
int Solver (int primes[5])
{
int i=1;
int x=0;
while (primes[x]!=0) {++x;} //work on the next one
if ((x>5) && Concat_Check(primes)) {return 1;} //solved array
for (i=3; i<=SIZE; i++) //try each value, if successful, return true
{
if (Is_Prime(i)) {primes[x]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
if ((Concat_Check (primes)) && Solver (primes)) {return 1;}
}
primes[x-1] = 0;
return 0;
}
I can't get the purpose of recursion in your code, seems a loop...
Anyway, maybe you forgot to increment x in loop, and the test seems incomplete.
for (i=3; i<=SIZE; i+=2) //try each value, if successful, return true
{
if (Is_Prime(i)) {
primes[x++]=i; cout<<"primes["<<x<<"] = "<<i<<endl;}
if ((Concat_Check (primes)) && Solver (primes)) {return 1;}
}
}

Using recursion to sum numbers

I have just been studying the concept of recursion and I thought that I would try a simple example. In the following code, I am attempting to take the numbers: 1, 2, 3, 4, 5, and add them together using recursion. I expected the result to be 15, but my code is returning 16.
What am I doing wrong?
Code:
static void Main(string[] args)
{
Console.WriteLine(Sum(5));
Console.Read();
}
static int Sum(int value)
{
if (value > 0)
{
return value + Sum(value - 1);
}
else
{
return 1;
}
}
You're returning 1 in the else clause. You should be returning 0:
else
{
return 0;
}
If the value is not greater than zero, why would you return one in the first place?
Your code executes as follows:
Sum --> 5
Sum --> 4
Sum --> 3
Sum --> 2
Sum --> 1
Sum --> 0
1 <---
2 <---
4 <---
7 <---
11 <---
16 <---
Check your base case.
Others already noted the error, and I will elaborate on recursion.
Although C# does not currently perform tail call optimization (although IL has special tail instruction), it's worth mentioning that tail recursion is generally a good thing.
Tail recursion is a special case of recursion in which the last operation of the function, the tail call, is a recursive call. Since the last call is the recursive call there is no need to preserve stack frame of the calling function and the compiler can easily use this information to generate machine instruction such that the stack doesn't grow at all. So it can basically turn recursive function into an iterative one.
Rewriting your code to support tail recursion can be done as follws:
static int Sum(int result, int value)
{
if(value == 0)
return result;
return Sum(result + 1, value - 1);
}
static int Sum(int value)
{
if (value > 0)
{
return value + Sum(value - 1);
}
else
{
return 0; //Change this.
}
}
That's because, when the value is = 0, you return 1. Then it get's added.
Sum's "else" clause should return 0.
I always prefer to put the terminating case(s) up front so they're obvious, and I have a violent near-psychopathic hatred of "if cond then return a else return b" constructs. My choice would be (making it clear that it won't work properly for negative numbers):
static unsigned int Sum(unsigned int value) {
if (value == 0)
return 0;
return value + Sum(value - 1);
}
I believe that's far more readable than a morass of braces and control flow.
The others have already answered that question, but when I work with recursion, one of the things I like to do to check that it works is to use check the base case and one additional case. I your case I would test it with 1, which would yield 2. Since this is obviously wrong you might want to check for 0 which is not going to use any recursion and so it should be obvious that the error lies in the base class.
In general recursion is easier to reason about, since you can list the limited number of things you need to check, but it does initially require a leap of faith since your intuition will be wrong. Just test the edge cases and trust the math it will never fail.
int summation(int num){
if (num==1)
return 1;
return summation(num-1)+num;
}
I'm pretty sure the problem is because you want your recursion to terminate when value == 1, and it's currently terminating when value == 0.
Your terminating expression is at issue. When value == 0 (or lower), it should return a 0 rather than 1. For sake of efficiency (which, let's admit it here, obviously isn't a concern, otherwise recursion wouldn't have been used for this task), you should terminate the recursion at value == 1 and return a literal 1 to save one unnecessary level of recursion.
using System;
using NUnit.Framework;
namespace Recursion
{
[TestFixture()]
public class Test
{
[Test()]
public void TestSum ()
{
Assert.AreEqual (Sum (new int[] { }), 0);
Assert.AreEqual (Sum (new int[] { 0 }), 0);
Assert.AreEqual (Sum (new int[] { 1 }), 1);
Assert.AreEqual (Sum (new int[] { 1, 2, 3, 4, 5 }), 15);
}
public int Sum(int[] head)
{
if (head.Length == 0) return 0;
int[] tail = new int[head.Length - 1];
for (int i = 1; i < head.Length; i++)
{
tail [i-1] = head [i];
}
return head[0] + Sum (tail);
}
}
}
It could also be written like this:
public static int sum(int n){
int total;
if(n==1){
total =1;
}else{
total = sum(n-1)+n;
}
return total;
}
Actually, I think you don't need to check case else because
public static int sum(int number){
if(number > 0){
return number + sum(--number);
}
return number; // return 0 so that's why you don't need check else condition
}
To begin at the end, a recursive Sum method looks like this:
// version 3
public static int Sum(int startRange, int endRange)
{
if (endRange > startRange)
{
return endRange + Sum(startRange, endRange - 1);
}
if (endRange < startRange)
{
return startRange + Sum(endRange, startRange - 1);
}
return endRange;
}
Hardcoding the startRange to be 0 gives us:
// version 2
public static int Sum(int range)
{
if (range > 0)
{
return range + Sum(0, range - 1);
}
if (range < 0)
{
return Sum(range, -1);
}
return range;
}
...and if you want to limit the method to positive numbers only, there's no need for a sign:
// version 1
public static unsigned int Sum(unsigned int range)
{
if (range > 0)
{
return range + Sum(0, range - 1);
}
return range;
}
I hope this helps give more of an insight into summing number ranges via recursion.
static int Sum(int[] addends)
{
if (addends.Length == 1)
{
return addends[0];
}
else
{
int tailIndex = addends.Length - 1;
var subArray = addends[0..tailIndex];
return addends[tailIndex] + Sum(subArray);
}
}
Try this code:
def sumr(n):
if n==0:
return n
return n+sumr(n-1)

Resources