External variables used in acc routine need to be in #pragma acc create( - openacc

I'm trying to compile a program in OpenACC and I'm having the following errors
NVC++-W-1056-External variables used in acc routine need to be in #pragma acc create() - i (sim_xy1.c: 1011)
NVC++-W-1056-External variables used in acc routine need to be in #pragma acc create() - j (sim_xy1.c: 1001)
NVC++-W-1056-External variables used in acc routine need to be in #pragma acc create() - j (sim_xy1.c: 993)
NVC++-W-1056-External variables used in acc routine need to be in #pragma acc create() - i (sim_xy1.c: 985)
NVC++-S-1065-Unsupported nested compute construct in compute construct or acc routine (sim_xy1.c: 976)
NVC++-S-1065-Unsupported nested compute construct in compute construct or acc routine (sim_xy1.c: 1028)
NVC++-S-1065-Unsupported nested compute construct in compute construct or acc routine (sim_xy1.c: 1042)
enter image description here
enter image description here

If you can provide a minimal reproducing example, it will be much easier to help. There's a lot of missing context since the code you screen shot is incomplete. However, this message may be the key:
NVC++-S-1065-Unsupported nested compute construct in compute construct or acc routine (sim_xy1.c: 1042)
This implies that you're nesting parallel loops and hence I assume that "produto_matriz_vetor" is a device routine is being called from this region?
Global variables in acc routines need to have a global device reference, in this case the loop index variables.
Note device vector routines can't use "parallel", but just the "loop" directive.

Related

SML Syntax Breakdown

I am trying to study SML (for full transparency this is in preparation for an exam (exam has not started)) and one area that I have been struggling with is higher level functions such as map and foldl/r. I understand that they are used in situations where you would use a for loop in oop languages (I think). What I am struggling with though is what each part in a fold or map function is doing. Here are some examples that if someone could break them down I would be very appreciative
fun cubiclist L = map (fn x=> x*x*x) L;
fun min (x::xs) = foldr (fn (a,b) => if (a < b) then a else b) x xs;
So if I could break down the parts I see and high light the parts I'm struggling with I believe that would be helpful.
Obviously right off the bat you have the name of the functions and the parameters that are being passed in but one question I have on that part is why are we just passing in a variable to cubiclist but for min we pass in (x::xs)? Is it because the map function is automatically applying the function to each part in the map? Also along with that will the fold functions typically take the x::xs parameters while map will just take a variable?
Then we have the higher order function along with the anonymous functions with the logic/operations that we want to apply to each element in the list. But the parameters being passed in for the foldr anonymous function I'm not quite sure about. I understand we are trying to capture the lowest element in the list and the then a else b is returning either a or b to be compared with the other elements in the list. I'm pretty sure that they are rutnred and treated as a in future comparisons but where do we get the following b's from? Where do we say b is the next element in the list?
Then the part that I really don't understand and have no clue is the L; and x xs; at the end of the respective functions. Why are they there? What are they doing? what is their purpose? is it just syntax or is there actually a purpose for them being there, not saying that syntax isn't a purpose or a valid reason, but does they actually do something? Are those variables that can be changed out with something else that would provide a different answer?
Any help/explanation is much appreciated.
In addition to what #molbdnilo has already stated, it can be helpful to a newcomer to functional programming to think about what we're actually doing when we crate a loop: we're specifying a piece of code to run repeatedly. We need an initial state, a condition for the loop to terminate, and an update between each iteration.
Let's look at simple implementation of map.
fun map f [] = []
| map f (x :: xs) = f x :: map f xs
The initial state of the contents of the list.
The termination condition is the list is empty.
The update is that we tack f x onto the front of the result of mapping f to the rest of the list.
The usefulness of map is that we abstract away f. It can be anything, and we don't have to worry about writing the loop boilerplate.
Fold functions are both more complex and more instructive when comparing to loops in procedural languages.
A simple implementation of fold.
fun foldl f init [] = init
| foldl f init (x :: xs) = foldl f (f init x) xs
We explicitly provide an initial value, and a list to operate on.
The termination condition is the list being empty. If it is, we return the initial value provided.
The update is to call the function again. This time the initial value is updated, and the list is the tail of the original.
Consider summing a list of integers.
foldl op+ 0 [1,2,3,4]
foldl op+ 1 [2,3,4]
foldl op+ 3 [3,4]
foldl op+ 6 [4]
foldl op+ 10 []
10
Folds are important to understand because so many fundamental functions can be implemented in terms of foldl or foldr. Think of folding as a means of reducing (many programming languages refer to these functions as "reduce") a list to another value of some type.
map takes a function and a list and produces a new list.
In map (fn x=> x*x*x) L, the function is fn x=> x*x*x, and L is the list.
This list is the same list as cubiclist's parameter.
foldr takes a function, an initial value, and a list and produces some kind of value.
In foldr (fn (a,b) => if (a < b) then a else b) x xs, the function is fn (a,b) => if (a < b) then a else b, the initial value is x, and the list is xs.
x and xs are given to the function by pattern-matching; x is the argument's head and xs is its tail.
(It follows from this that min will fail if it is given an empty list.)

Continued fractions in f# using seq

Hi is there a way to solve continued fractions in f# using seq , not list?
let cfToScalar cf = List.foldBack (fun elem acc -> float elem + (1.0 / float acc)) cf System.Double.MaxValue
Someting like that but i want to use seq instead of lists
As I said in my comment, you can just change List.foldback to Seq.foldback and your function will now accept sequences. Also note as Sehnsucht said, foldback's current implementation requires a conversion to an array so an input that is an infinite sequence will not work.
On a side note, since F# 4.0's normalization of the collection modules swapping out one module for another in general now possible with most functions in the List/Array/Seq modules.

Type-independent memory reallocation in fortran

I try to construct a subroutine to reallocate memory for a type-independent allocatable array like this:
subroutine reallocate(vector, num)
implicit none
class(*), dimension(:), allocatable, intent(inout) :: vector
integer :: num
class(*), dimension(:), allocatable :: tmp
integer :: lb, ub, ii_
if (allocated(vector)) then
ub = max(ubound(vector, 1), ub)
lb = min(lbound(vector, 1), lb)
if (ub .GT. ubound(vector, 1) .OR. lb .LT. lbound(vector, 1)) then
allocate(tmp(ub:lb), source=vector)
tmp(lbound(vector,1):ubound(vector,1)) = vector
call move_alloc(tmp, vector)
else
return
end if
else
allocate(vector(num:num), source=vector)
return
end if
return
end subroutine
For example, let's say I have a type(type1), allocatable :: v allocated within the indices -1 and 4, and I call reallocate(v, 6). After that I want v to be allocated between -1 and 6.
So, problem here comes when vector is already allocated, and I want to keep the information already stored in the vector by copying it to a newly reallocated temporal array (line that reads tmp(lbound(vector,1):ubound(vector,1)) = vector). gfortran complains: "Error: Nonallocatable variable must not be polymorphic in intrinsic assignment at (1) - check that there is a matching specific subroutine for '=' operator."
Is what I intent allowed in the Fortran 2003 standard? What would be the way to do this?
There is no way to write such a type agnostic reallocation procedure in Fortran 2003, or Fortran 2008.
Your options are:
push the allocate statements that do the (re-)allocation, back up into the scope where the type is known, effectively repeating code for each reallocation;
explicitly rewrite the reallocation procedure for each type of interest; or
write the source code for the reallocation once generically, but then use INCLUDE tricks or similar to explicitly instantiate the procedure for each type of interest.
CLASS(*) is typically not appropriate as a generic programming facility. Even if it was possible to write the body of that procedure, it is impossible to usefully call.
(Note that the example code shown references undefined and potentially unallocated variables.)

transferring an imperative for-loop into idiomatic haskell

I have some difficulties to transfer imperative algorithms into a functional style. The main concept that I cannot wrap my head around is how to fill sequences with values according to their position in the sequence. How would an idiomatic solution for the following algorithm look in Haskell?
A = unsigned char[256]
idx <- 1
for(i = 0 to 255)
if (some_condition(i))
A[i] <- idx
idx++
else
A[i] = 0;
The algorithm basically creates a lookup table for the mapping function of a histogram.
Do you know any resources which would help me to understand this kind of problem better?
One of the core ideas in functional programming is to express algorithms as data transformations. In a lazy language like Haskell, we can even go a step further and think of lazy data structures as reified computations. In a very real sense, Haskell's lists are more like loops than normal linked lists: they can be calculated incrementally and don't have to exist in memory all at once. At the same time, we still get many of the advantages of having a data type like that ability to pass it around and inspect it with pattern matching.
With this in mind, the "trick" for expressing a for-loop with an index is to create a list of all the values it can take. Your example is probably the simplest case: i takes all the values from 0 to 255, so we can use Haskell's built-in notation for ranges:
[0..255]
At a high level, this is Haskell's equivalent of for (i = 0 to 255); we can then execute the actual logic in the loop by traversing this list either by a recursive function or a higher-order function from the standard library. (The second option is highly preferred.)
This particular logic is a good fit for a fold. A fold lets us take in a list item by item and build up a result of some sort. At each step, we get a list item and the value of our built-up result so far. In this particular case, we want to process the list from left to right while incrementing an index, so we can use foldl; the one tricky part is that it will produce the list backwards.
Here's the type of foldl:
foldl :: (b -> a -> b) -> b -> [a] -> b
So our function takes in our intermediate value and a list element and produces an updated intermediate value. Since we're constructing a list and keeping track of an index, our intermediate value will be a pair that contains both. Then, once we have the final result, we can ignore the idx value and reverse the final list we get:
a = let (result, _) = foldl step ([], 1) [0..255] in reverse result
where step (a, idx) i
| someCondition i = (idx:a, idx + 1)
| otherwise = (0:a, idx)
In fact, the pattern of transforming a list while keeping track of some intermediate state (idx in this case) is common enough so that it has a function of its own in terms of the State type. The core abstraction is a bit more involved (read through ["You Could Have Invented Monads"][you] for a great introduction), but the resulting code is actually quite pleasant to read (except for the imports, I guess :P):
import Control.Applicative
import Control.Monad
import Control.Monad.State
a = evalState (mapM step [0..255]) 1
where step i
| someCondition i = get <* modify (+ 1)
| otherwise = return 0
The idea is that we map over [0..255] while keeping track of some state (the value of idx) in the background. evalState is how we put all the plumbing together and just get our final result. The step function is applied to each input list element and can also access or modify the state.
The first case of the step function is interesting. The <* operator tells it to do the thing on the left first, the thing on the right second but return the value on the left. This lets us get the current state, increment it but still return the value we got before it was incremented. The fact that our notion of state is a first-class entity and we can have library functions like <* is very powerful—I've found this particular idiom really useful for traversing trees, and other similar idioms have been quite useful for other code.
There are several ways to approach this problem depending on what data structure you want to use. The simplest one would probably be with lists and the basic functions available in Prelude:
a = go 1 [] [0..255]
where
go idx out [] = out
go idx out (i:is) =
if condition i
then go (idx + 1) (out ++ [idx]) is
else go idx (out ++ [0]) is
This uses the worker pattern with two accumulators, idx and out, and it traverses down the last parameter until no more elements are left, then returns out. This could certainly be converted into a fold of some sort, but in any case it won't be very efficient, appending items to a list with ++ is very inefficient. You could make it better by using idx : out and 0 : out, then using reverse on the output of go, but it still isn't an ideal solution.
Another solution might be to use the State monad:
a = flip runState 1 $ forM [0..255] $ \i -> do
idx <- get
if condition i
then do
put $ idx + 1 -- idx++
return idx -- A[i] = idx
else return 0
Which certainly looks a lot more imperative. The 1 in flip runState 1 is indicating that your initial state is idx = 1, then you use forM (which looks like a for loop but really isn't) over [0..255], the loop variable is i, and then it's just a matter of implementing the rest of the logic.
If you want to go a lot more advanced you could use the StateT and ST monads to have an actual mutable array with a state at the same time. The explanation of how this works is far beyond the scope of this answer, though:
import Control.Monad.State
import Control.Monad.ST
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
a :: V.Vector Int
a = runST $ (V.freeze =<<) $ flip evalStateT (1 :: Int) $ do
a' <- lift $ MV.new 256
lift $ MV.set a' 0
forM_ [0..255] $ \i -> do
when (condition i) $ do
idx <- get
lift $ MV.write a' i idx
put $ idx + 1
return a'
I simplified it a bit so that each element is set to 0 from the start, we begin with an initial state of idx = 1, loop over [0..255], if the current index i meets the condition then get the current idx, write it to the current index, then increment idx. Run this as a stateful operation, then freeze the vector, and finally run the ST monad side of things. This allows for an actual mutable vector hidden safely within the ST monad so that the outside world doesn't know that to calculate a you have to do some rather strange things.
Explicit recursion:
a = go 0 1
where go 256 _ = []
go i idx | someCondition i = idx : go (i+1) (idx+1)
| otherwise = 0 : go (i+1) idx
Unfolding: (variant of the explicit recursion above)
a = unfoldr f (0,1)
where f (256,_) = Nothing
f (i,idx) | someCondition i = Just (idx,(i+1,idx+1))
| otherwise = Just (0 ,(i+1,idx ))
Loops can usually be expressed using different fold functions. Here is a solution which uses foldl(you can switch to foldl' if you run into a stackoverflow error):
f :: (Num a) => (b -> Bool) -> a -> [b] -> [a]
f pred startVal = reverse . fst . foldl step ([], startVal)
where
step (xs, curVal) x
| pred x = (curVal:xs, curVal + 1)
| otherwise = (0:xs, curVal)
How to use it? This function takes a predicate (someCondition in your code), the initial value of an index and a list of element to iterate over. That is, you can call f someCondition 1 [0..255] to obtain the result for the example from your question.

How do I check where my code gets stuck in Erlang?

I'm trying to write a function that receives a list, finds the highest value integer in the list, and then divides all the other integers in the list by that value.
Unfortunately, my code gets stuck somewhere. If this were python for example I could easily write a couple different "print"s and see where it gets stuck. But how do you do that in Erlang?
Here is the code.
highest_value([], N) ->
if
N =:= 0 ->
'Error! No positive values.'
end,
N;
highest_value([H|T], N) when H > N, H > 0 ->
highest_value([T], H);
highest_value([_|T], N) ->
highest_value([T], N).
divide(_L) -> [X / highest_value(_L, 0) || X <- _L].
For prints you could just use io:format/2. Same thing.
highest_value([H|T], N) when H > N, H > 0 ->
io:format(">>> when H bigger than N~n"),
io:format(">>> H: ~p, T: ~p, N: ~p ~n", [H, T, N]),
highest_value([T], H);
highest_value(List) ->
highest_value(List, 0).
EDIT
One thing you are getting wrong is [H | T] syntax. H, or head, is the first element in the list. T stands for tail, or "rest of the list". And like the name suggests, tail is a list (could be an empty list, but a list nonetheless). So when you are doing you recursion you don't need to put T inside a new list.
highest_value([H|T], N) when H > N ->
highest_value(T, H);
highest_value([_|T], N) ->
highest_value(T, N).
In your old code you called:
highest_value([T], N).
which created a new list with one element, like [[2,3,4,5]]. If you head-tail this, you get this only-element-list as the head, and an empty list as the tail.
Also, in your first function clause you have an atom 'Error! No positive values.' (singe quotes means this is just a long atom, and not a string) which is never returned (you will always return N). If you would like to return either some atom, or N, depending on value of N you could just extend your use of function clauses
highest_value([], 0) ->
'Error! No positive values.'
highest_value([], N) ->
N;
[...]
And you have to initialize your function with 0, which could be considered a bad pattern. You could write and use highest_value/1 which does that for you
highest_value(List) ->
highest_value(List, 0).
Or even use a modification of this algorithm: since the largest number will be one of the numbers in the list, you could use the first element as the function initialization.
highest_value(_List = [First|T]) when First > 0 ->
highest_value(T, First).
This assumes that handling negative numbers is something you don't care about right now.
While debugging via print statements is common and even sometimes useful, and io:format can be used for this purpose in Erlang as already noted, Erlang provides powerful built-in tracing capabilities you should use instead.
Let's say your highest_value/2 and divide/1 functions reside in a module named hv. First, we compile hv in the Erlang shell:
1> c(hv).
{ok,hv}
Next, we use Erlang's dbg module to enable tracing on the hv functions:
2> dbg:tracer().
{ok,<0.41.0>}
3> dbg:p(self(),call).
{ok,[{matched,nonode#nohost,26}]}
4> dbg:tpl(hv,c).
{ok,[{matched,nonode#nohost,5},{saved,c}]}
In command 2 we enable debug tracing and in command 3 we indicate that we want to trace function calls in our current process (returned by self()). In command 4 we create a call trace, using the built-in c trace specification, on all functions in the hv module.
Once debug tracing is enabled, we call hv:divide/1 and the trace output begins:
5> hv:divide([4,8,12,16]).
(<0.34.0>) call hv:divide([4,8,12,16]) ({erl_eval,do_apply,6})
(<0.34.0>) call hv:'-divide/1-lc$^0/1-0-'([4,8,12,16],[4,8,12,16]) ({erl_eval,
do_apply,
6})
(<0.34.0>) call hv:highest_value([4,8,12,16],0) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([[8,12,16]],4) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([[]],[8,12,16]) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([[]],[8,12,16]) ({hv,'-divide/1-lc$^0/1-0-',2})
...
First, note that I abbreviated the trace output because at the ... point it's already in an infinite loop, and the remainder of the trace is identical to the two statements prior to the ....
What does the trace output tell us? The first line shows the invocation of the divide/1 function, and the second line shows the call to the list comprehension inside divide/1. We then see calls to highest_value/2, first with the full list and N set to 0. The next call is where it gets interesting: because your code passes [T] rather than T as the first argument in the recursive invocation of highest_value/2, H has the value [8,12,16], which Erlang treats as being greater than the current N value of 4, so the next recursive call is:
highest_value([T], [8,12,16]).
and because T is [], this turns into:
highest_value([[]], [8,12,16]).
Here, H is [], and T is also []. H is not greater than [8,12,16], so all remaining recursive invocations after this point are identical to this one, and the recursion is infinite.
To fix this, you need to pass T correctly as already noted:
highest_value([H|T], N) when H > N, H > 0 ->
highest_value(T, H);
highest_value([_|T], N) ->
highest_value(T, N).
Then recompile, which also reloads your module, and because of that you'll also need to set up your debug tracing again:
5> c(hv).
{ok,hv}
6> dbg:tpl(hv,c).
{ok,[{matched,nonode#nohost,5},{saved,c}]}
7> hv:divide([4,8,12,16]).
(<0.34.0>) call hv:divide([4,8,12,16]) ({erl_eval,do_apply,6})
(<0.34.0>) call hv:'-divide/1-lc$^0/1-0-'([4,8,12,16],[4,8,12,16]) ({erl_eval,
do_apply,
6})
(<0.34.0>) call hv:highest_value([4,8,12,16],0) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([8,12,16],4) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([12,16],8) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([16],12) ({hv,'-divide/1-lc$^0/1-0-',2})
(<0.34.0>) call hv:highest_value([],16) ({hv,'-divide/1-lc$^0/1-0-',2})
** exception error: no true branch found when evaluating an if expression
in function hv:highest_value/2 (/tmp/hv.erl, line 5)
in call from hv:'-divide/1-lc$^0/1-0-'/2 (/tmp/hv.erl, line 15)
Tracing now shows that highest_value/2 is working as expected, but we now hit a new problem with the if statement, and the fix for this is already explained in another answer so I won't repeat it here.
As you can see, Erlang's tracing is far more powerful than using "print debugging".
It can be turned on and off interactively in the Erlang shell as needed.
Unlike debugging in other languages, debug tracing requires no special compilation flags for your modules.
Unlike with debug print statements, you need not change your code and recompile repeatedly.
What I've shown here barely scratches the surface as far as Erlang's tracing capabilities go, but it was more than enough to find and fix the problems.
And finally, note that using the lists:max/1 standard library call you can more easily achieve what your module is trying to do:
divide(L) ->
case lists:max(L) of
N when N > 0 ->
[V/N || V <- L];
_ ->
error(badarg, [L])
end.

Resources