doParallel does not recognize functions in global environments with multiple sourcings - parallel-processing

I have a quick question with respect to the doParallel package in R. I have an optimize.R file where it contains roughly 18 functions A1, A2, A3, A4, ..., A18 within it, and A18 would basically contain all the functions A1, A2, A3,..., A17. I have another result.R file, where I used import every functions within optimize.R into the Global Environment of RStudio (located on the upper-right corner of RStudio). In addition, within result.R file, I have a function F that calls function E under the loop foreach(..., .export = c(".GlobalEnv")) %dopar% {do something}.
Now, in the final.R file (which is the main file), I use source(/C:/Users/[Name]/Desktop/result.R, echo = F). Within final.R, I have a function res that calls function F.
However, when I execute this final.R file, I got the error message: task 1: cannot find variable C , which is weird because C is a function, not a variable. Also, it seems to me the .export = c(".GlobalEnv") fails to work, as function C is already in the Global Environment. Can anyone suggest some ways to overcome this kind of issue? I tried to export
Flow of the 3 files.
final.R file
source(/C:/Users/[Name]/Desktop/result.R, echo = F)
library(doParallel)
registerDoParallel(4)
res <- function(var_x, var_y) {
best.rev <- foreach(i=1:5, .export = c(".GlobalEnv")) %dopar% {
F(var_x, var_y)
do something else
}
output <- best.rev
}
result.R file
source(/C:/Users/[Name]/Desktop/optimize.R, echo = F)
F <- function(var1, var2) {
E(var1, var2)
do something else
}
optimize.R file
A1 <- function(x1, x2) {....}
A2 <- function(x1, x3, x4) {...}
A3 <- function(x1, x2, x3) {...}
....
A18 <- function(x1,x2,x3,x4,...,x8) {
a1 <- A1(x1, x2)
a2 <- A2(a1, x3, x4)
a3 <- A3(a1, a2, x3)
return(list(a1, a2, a3))
}

Related

sed grep to group values on multi lines

I have a file that can resembles to something like that:
function(a, b, c1, d1, e1, f1);
function(a, b, c2
,d2, e2
f2);
useless things
function(a, b, c3,
/* something lol */
// something else */
d3, e3
, f3
);
The idea is to get something like:
c1
d1
e1
f1
c2
d2
e2
f2
c3
d3
e3
f3
I am using sed to remove things that are useless between each function, so I came with
sed -n '/function(/,/);/p' file
Here I get the three functions without the useless things.
Now I am tring to put the thing between function into one line maybe delete also the things after // or between /* */. But I don't know how can I "concat" things so I can get 3 lines instead of 10
Using grep
grep -o '[a-z][0-9]' < input_File
Demo :
$cat file.txt
function(a, b, c1, d1, e1, f1);
function(a, b, c2
,d2, e2
f2);
useless things
function(a, b, c3,
/* something lol */
// something else */
d3, e3
, f3
);
$grep -o '[a-z][0-9]' < file.txt
c1
d1
e1
f1
c2
d2
e2
f2
c3
d3
e3
f3
$

Error in Eiffel implementation of Burnikel and Ziegler algorithm 2

I need another set of eyes to tell me what is wrong with my Eiffel implementation of Burnikel and Ziegler's division, specifically "Algorithm 2 - 3n/2n". The Eiffel feature is shown below. The type "like Current" is an ARRAYED_LIST [NATURAL_8]. In other words, the implementation uses digits (i.e. limbs) containing 8-bit values, so numbers are in base-256. A manual trace of a failing call follows. (Sorry the arguments are so large, but I cannot reproduce the error with shorter values.) Execution follows step 3b in this case.
Here's the problem. The algorithm seems to be fine to Step 5, where the remainder "r" ends up with more digits then the divisor. I believe the error is in step 3b, perhaps with the call to feature `ones' which is "supposed" to supply a value that is "Beta^n - 1". (Maybe I do not understand B&Z's "Beta^n" notation.
Here is the Eiffel code:
three_by_two_divide (a, a3, b: like Current): TUPLE [quot, rem: like Current]
-- Called by `two_by_one_divide'. It has similar structure as
-- `div_three_halves_by_two_halfs', but the arguments to this
-- function have type {JJ_BIG_NATURAL} instead of like `digit'.
-- See Burnikel & Zieler, "Fast Recursive Division", pp 4-8,
-- Algorithm 2.
require
n_not_odd: b.count >= div_limit and b.count \\ 2 = 0
b_has_2n_digits: b.count = a3.count * 2
a_has_2n_digits: a.count = a3.count * 2
local
n: INTEGER
a1, a2: like Current
b1, b2: like Current
tup: TUPLE [quot, rem: like Current]
q, q1, q2, r, r1: like Current
c, d: like Current
do
n := b.count // 2
-- 1) Split `a'
a1 := new_sub_number (n + 1, a.count, a)
a2 := new_sub_number (1, n.max (1), a)
-- 2) Split `b'.
b1 := new_sub_number (n + 1, b.count, b)
b2 := new_sub_number (1, n.max (1), b)
-- 3) Distinguish cases.
if a1 < b1 then
-- 3a) compute Q = floor ([A1,A2] / B1 with remainder.
if b1.count < div_limit then
tup := school_divide (a, b1)
else
tup := two_by_one_divide (a, b1)
end
q := tup.quot
r1 := tup.rem
else
-- 3b) Q = beta^n - 1 and ...
q := ones (n)
-- ... R1 = [A1,A2] - [B1,0] + [0,B1] = [A1,A2] - QB1.
r1 := a + b1
if n > 1 then
b1.shift_left (n)
else
b1.bit_shift_left (zero_digit.bit_count // 2)
end
r1.subtract (b1)
end
-- 4) D = Q * B2
d := q * b2
-- 5) R1 * B^n + A3 - D. (The paper says "a4".)
r1.shift_left (n)
r := r1 + a3 - d
-- 6) As long as R < 0, repeat
from
until not r.is_negative
loop
r := r + b
q.decrement
end
check
remainder_small_enough: r.count <= b.count
-- because remainder must be less than divisor.
end
Result := [q, r]
ensure
-- n_digit_remainder: Result.rem.count = b.count // 2
quotient_has_correct_count: Result.quot.count <= b.count // 2
end
In the trace, the arrow points to a line I believe is bad, but I don't know what to do with it. Here is the trace:
three_by_two_divide (a = [227,26,41,95,169,93,135,110],
a3 = [92,164,19,39],
b = [161,167,158,41,164,0,0,0])
n := b.count // 2 = 4
-- 1) Split `a'.
a1 := new_sub_number (n + 1, a.count, a) = [227,26,41,95]
a2 := new_sub_number (1, n.max (1), a) = [169,93,135,110]
-- 2) Split `b'.
b1 := new_sub_number (n + 1, b.count, b) = [161,167,158,41]
b2 := new_sub_number (1, n.max (1), b) = [164,0,0,0]
-- 3b) Q = beta^n -1 and ...
--> q := ones (4) = [255,255,255,255] <-- Is this the error?
-- ... R1 = [A1,A2] - [B1,0] + [0,B1].
r1 := a + b1 = [227,26,41,96,75,5,37,151]
b1.shift_left (n) = [161,167,158,41,0,0,0,0]
r1.subtract (b1) = [65,114,139,55,75,5,37,151]
d := q * b2 = [163,255,255,255,92,0,0,0]
r1.shift_left (n) = [227,25,135,184,172,220,37,151,0,0,0,0] -- too big!
r := r1 + a3 - d -= [227,25,135,184,8,220,37,152,0,164,19,39] -- too big!
I know this is long, but any help is appreciated.
I would suggest to check that r1 = [65,114,139,55,75,5,37,151] is still the same before doing r1.shift_left (n). There are two options:
d := q * b2 affects r1 while it should not. Most probably there is some aliasing, i.e. r1 is aliased with some other variable that is updated and this aliasing should be removed.
r1 is still the same after d := q * b2. The issue is with shift_left that fails to (re)initialize some data or uses global data that it should not.

Ocaml Sort Program

I wrote these lines of code so as to sort the input of 5 numbers.
But when i compile and run it, then there is an error like - "int_of_string"
I do not know why this is not running. I am new to Ocaml.
let sort2 (a, b) = if a<b
then (a, b)
else (b, a)
let sort3 (a, b, c) =
let (a, b) = sort2(a, b) in
let (b, c) = sort2(b, c) in
let (a, b) = sort2(a, b) in
(a, b, c)
let sort5 (a, b, c, d, e) =
let (a, b, c) = sort3 (a, b, c) in
let (c, d, e) = sort3(c, d, e) in
let (a, b, c) = sort3 (a, b, c) in
(a, b, c, d, e)
let _ =
let a = read_int () in
let b = read_int () in
let c = read_int () in
let d = read_int () in
let e = read_int () in
let (a, b, c, d, e) = sort5 (a, b, c, d, e) in
print_int a; print_newline ();
print_int b; print_newline ();
print_int c; print_newline ();
print_int d; print_newline ();
print_int e; print_newline ()
Exception: Failure "int_of_string".
The exception happens when you type a line of input that cannot be parsed as an integer, such as an empty line. This must be what happened during your tests.
Error handling
If you want a little bit of robustness, you have to take into account that the input can be malformed. You can catch the runtime error to handle unexpected inputs:
# let maybe_read_int () = try Some (read_int ()) with Failure _ -> None;;
val maybe_read_int : unit -> int option = <fun>
The value returned from the above function is an int option.
Failure
# maybe_read_int ();;
foo
- : int option = None
Success
# maybe_read_int ();;
42
- : int option = Some 42
Reading multiple integers
You can't just use the above function like in your example because some of your variables would be bound to None (in that case, this is no better that letting the exception bubble up). Instead, you may want to read as many lines as necessary until you get 5 integers:
let rec read_n_ints n =
if (n > 0) then
match (maybe_read_int ()) with
| None -> read_n_ints n
| Some v -> v :: (read_n_ints (n - 1))
else [];;
# read_n_ints 3;;
0
foo
bar
1
2
- : int list = [0; 1; 2]
Now that you have a list of integers, you can bind them to variables using pattern matching. Note that we have to be exhaustive and consider cases that should not happen:
# match (read_n_ints 5) with
| [ a; b; c; d; e] -> (a + b + c + d + e)
| _ -> raise (Failure "Failed to read 5 integers");;
3
foo
2
10
30
ii
r
90
3
- : int = 136

UITextField int data type xcode

I am trying to get a mathmetical equation to recognise a + /- sign of an integer (either -1 or +1) entered in a UItextfield (s1, s2). So if the user enters different signs the equations will be subtracted from each other.
It seems that the sign is not being recognised for some reason and the program just adds d1 and d2.
-(IBAction)calculateD:(id)sender{
float n1, r1, n2, r1, d, d1, d2;
int s1, s2;
s1= [textfieldS1.text intvalue]; //etc for all variables
d1 = s1 * ((n1-1)/r1);
d2 = s2 * ((n2-1)/r2);
if (s1 != s2) { d = d1 - d2;}
else { d = d1 + d2;
}}
Any problems apparent in this code please?
I have no idea what you are trying to do here. Variables are not initialized and there is no specific reference to actual UITextField inside of the -calculateD: method. With this said, here are some hints, hope it will come in hand.
The signs s1, s2 are actually taken twice into a consideration. Once to produce d1, d2, and later to decide the (s1 != s2). Because of this, the latter will make sure you add two numbers of the same sign, possibly negating what you really want to obtain here. Example:
say that s1=+1, s1=+1, then you got d = ((n1-1)/r1) + ((n2-1)/r2);
say that s1=+1, s1=-1, then you got d = ((n1-1)/r1) + ((n2-1)/r2); the same as before;
Just drop the if, and leave a single: d = d1 + d2.

haskell matrix implemetation performance

I heard a lot about amazing performance of programs written in Haskell, and wanted to make some tests. So, I wrote a 'library' for matrix operations just to compare it's performance with the same stuff written in pure C.
First of all I tested 500000 matrices multiplication performance, and noticed that it was... never-ending (i. e. ending with out of memory exception after 10 minutes of so)! After studying haskell a bit more I managed to get rid of laziness and the best result I managed to get is ~20 times slower than its equivalent in C.
So, the question: could you review the code below and tell if its performance can be improved a bit more? 20 times is still disappointing me a bit.
import Prelude hiding (foldr, foldl, product)
import Data.Monoid
import Data.Foldable
import Text.Printf
import System.CPUTime
import System.Environment
data Vector a = Vec3 a a a
| Vec4 a a a a
deriving Show
instance Foldable Vector where
foldMap f (Vec3 a b c) = f a `mappend` f b `mappend` f c
foldMap f (Vec4 a b c d) = f a `mappend` f b `mappend` f c `mappend` f d
data Matr a = Matr !a !a !a !a
!a !a !a !a
!a !a !a !a
!a !a !a !a
instance Show a => Show (Matr a) where
show m = foldr f [] $ matrRows m
where f a b = show a ++ "\n" ++ b
matrCols (Matr a0 b0 c0 d0 a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3)
= [Vec4 a0 a1 a2 a3, Vec4 b0 b1 b2 b3, Vec4 c0 c1 c2 c3, Vec4 d0 d1 d2 d3]
matrRows (Matr a0 b0 c0 d0 a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3)
= [Vec4 a0 b0 c0 d0, Vec4 a1 b1 c1 d1, Vec4 a2 b2 c2 d2, Vec4 a3 b3 c3 d3]
matrFromList [a0, b0, c0, d0, a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3]
= Matr a0 b0 c0 d0
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
matrId :: Matr Double
matrId = Matr 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
normalise (Vec4 x y z w) = Vec4 (x/w) (y/w) (z/w) 1
mult a b = matrFromList [f r c | r <- matrRows a, c <- matrCols b] where
f a b = foldr (+) 0 $ zipWith (*) (toList a) (toList b)
First, I doubt that you'll ever get stellar performance with this implementation. There are too many conversions between different representations. You'd be better off basing your code on something like the vector package. Also you don't provide all your testing code, so there are probably other issues that we can't here. This is because the pipeline of production to consumption has a big impact on Haskell performance, and you haven't provided either end.
Now, two specific problems:
1) Your vector is defined as either a 3 or 4 element vector. This means that for every vector there's an extra check to see how many elements are in use. In C, I imagine your implementation is probably closer to
struct vec {
double *vec;
int length;
}
You should do something similar in Haskell; this is how vector and bytestring are implemented for example.
Even if you don't change the Vector definition, make the fields strict. You should also either add UNPACK pragmas (to Vector and Matrix) or compile with -funbox-strict-fields.
2) Change mult to
mult a b = matrFromList [f r c | r <- matrRows a, c <- matrCols b] where
f a b = Data.List.foldl' (+) 0 $ zipWith (*) (toList a) (toList b)
The extra strictness of foldl' will give much better performance in this case than foldr.
This change alone might make a big difference, but without seeing the rest of your code it's difficult to say.
Answering my own question just to share new results I got yesterday:
I upgraded ghc to the most recent version and performance became indeed not that bad (only ~7 times worse).
Also I tried implementing the matrix in a stupid and simple way (see the listing below) and got really acceptable performance - only about 2 times slower than C equivalent.
data Matr a = Matr ( a, a, a, a
, a, a, a, a
, a, a, a, a
, a, a, a, a)
mult (Matr (!a0, !b0, !c0, !d0,
!a1, !b1, !c1, !d1,
!a2, !b2, !c2, !d2,
!a3, !b3, !c3, !d3))
(Matr (!a0', !b0', !c0', !d0',
!a1', !b1', !c1', !d1',
!a2', !b2', !c2', !d2',
!a3', !b3', !c3', !d3'))
= Matr ( a0'', b0'', c0'', d0''
, a1'', b1'', c1'', d1''
, a2'', b2'', c2'', d2''
, a3'', b3'', c3'', d3'')
where a0'' = a0 * a0' + b0 * a1' + c0 * a2' + d0 * a3'
b0'' = a0 * b0' + b0 * b1' + c0 * b2' + d0 * b3'
c0'' = a0 * c0' + b0 * c1' + c0 * c2' + d0 * c3'
d0'' = a0 * d0' + b0 * d1' + c0 * d2' + d0 * d3'
a1'' = a1 * a0' + b1 * a1' + c1 * a2' + d1 * a3'
b1'' = a1 * b0' + b1 * b1' + c1 * b2' + d1 * b3'
c1'' = a1 * c0' + b1 * c1' + c1 * c2' + d1 * c3'
d1'' = a1 * d0' + b1 * d1' + c1 * d2' + d1 * d3'
a2'' = a2 * a0' + b2 * a1' + c2 * a2' + d2 * a3'
b2'' = a2 * b0' + b2 * b1' + c2 * b2' + d2 * b3'
c2'' = a2 * c0' + b2 * c1' + c2 * c2' + d2 * c3'
d2'' = a2 * d0' + b2 * d1' + c2 * d2' + d2 * d3'
a3'' = a3 * a0' + b3 * a1' + c3 * a2' + d3 * a3'
b3'' = a3 * b0' + b3 * b1' + c3 * b2' + d3 * b3'
c3'' = a3 * c0' + b3 * c1' + c3 * c2' + d3 * c3'
d3'' = a3 * d0' + b3 * d1' + c3 * d2' + d3 * d3'

Resources