Retrievieng SharedArray on Julia 0.4 - arrays

Edit: I did try using fetch()
I seem to have break something in Julia this week. I had played with the SharedArray type on a computer with 12 threads (6 doble thread cpu), I maneged to get a result and to print it and save it as matrix text file without problem, more or less following the instructions on http://docs.julialang.org/en/release-0.4/manual/parallel-computing/#shared-arrays. I had this routine where I initialized a number of workers and an Array, pass them as argument to a function, and expected to obtain a SharedArray with numerical values in return. It went more or less like this
addprocs(11)
BCero=rand(128,128)
ConjuntoX=Array[]
for j,k=1:128
push!(ConjuntoX, [j,k])
end
function obtenerKernelParalell(LasB::Array, lasX::Array, jmax::Int)
result=SharedArray(Float64,(jmax,jmax))
#sync #parallel for j=1:jmax
xj=lasX[j]
for k=1:j
xk=lasX[k]
for l=1:jmax
xl=lasX[l]
result[j,k]+= LasB[(xk-xl+xconstante)...]*LasB[(xj-xl+xconstante)...]
end
end
end
end
KSuaveParalel=obtenerKernelParalell(BceroSuave, ConjuntoX,128);
What I did get after runing this for the first time was an array that behaved itself like a normal Array. If I typed KSuaveParalel[3,12] I obtained a value. But now I get the next thing from the REPL:
KSuaveParalel
11-element Array{Any,1}:
RemoteRef{Channel{Any}}(2,1,122)
RemoteRef{Channel{Any}}(3,1,123)
RemoteRef{Channel{Any}}(4,1,124)
RemoteRef{Channel{Any}}(5,1,125)
RemoteRef{Channel{Any}}(6,1,126)
RemoteRef{Channel{Any}}(7,1,127)
RemoteRef{Channel{Any}}(8,1,128)
RemoteRef{Channel{Any}}(9,1,129)
RemoteRef{Channel{Any}}(10,1,130)
RemoteRef{Channel{Any}}(11,1,131)
RemoteRef{Channel{Any}}(12,1,132)
So I got an array of References... and I do not know how to get its values. Also using fetch() doesn't seem to work.
What is going on here?

Edit:
You need to make sure you return the array in the function.
i.e. return result
You will want to call fetch() on each Remote Reference object to wait and get the value that will be returned.
[fetch(x) for x in KSuaveParalel]
The RemoteRef object is returned immediately (i.e. before the computation has actually been done). See this answer (Julia Parallel macro does not seem to work) and the docs for more info.
http://docs.julialang.org/en/release-0.4/stdlib/parallel/#Base.fetch

Okey, sort of figured a way to do it, but is not probably the most efficient way to do it: If I convert INSIDE the function the result from SharredArray to Array, I get the (apparently) correct result:
function obtenerKernelParalell(LasB::Array, lasX::Array, jmax::Int)
result=SharedArray(Float64,(jmax,jmax))
#sync #parallel for j=1:jmax
xj=lasX[j]
for k=1:j
xk=lasX[k]
for l=1:jmax
xl=lasX[l]
result[j,k]+= LasB[(xk-xl+xconstante)...]*LasB[(xjxl+xconstante)...]
end
end
end
result=Array(result)
return result
end
Is this because the conversion does the fetch() with the right settings automatically?

Related

Passing Ruby array elements to a method

I'm not a programmer, but I find myself writing some simple ruby and aren't sure about a few things.
I have the following function
def resolve_name(ns_name)
ip = Resolv.getaddress(ns_name)
return ip
end
and the array
array = ['ns-1.me.com', 'ns-2.me.com']
What I want to do is to pass every element in the array to the function to be evaluated, and spit out to... something. Probably a variable. Once I have the resolved IPs I'll be passing them to an erb template. Not quite sure yet how to handle when there may be 1 to 4 possible results either.
What I want think I need to do is do an each.do and typecast to string into my function, but I haven't been able to figure out how to actually do that or phrase my problem properly for google to tell me.
http://ruby-doc.org/core-2.0.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion Doesn't quite have what I'm looking for.
irb(main):010:0> resolved = resolve_name(array)
TypeError: no implicit conversion of Array into String
Any suggestions?
Take a look at the documentation for ruby's Enumerable, which arrays implement. What you're looking for is the map method, which takes each element of an enumerable (i.e. an array) and passes it to a block, returning a new array with the results of the blocks. Like this:
array.map{|element| resolve_name(element) }
As an aside, in your method, you do not need to use a local variable if all you're doing with it is returning its value; and the return statement is optional - ruby methods always return the result of the last executed statement. So your method could be shortened to this:
def resolve_name(ns_name)
Resolv.getaddress(ns_name)
end
and then you really all it's doing is wrapping a method call in another. So ultimately, you can just do this (with array renamed to ns_names to make it self-explanatory):
ns_names = ['ns-1.me.com', 'ns-2.me.com']
ip_addresses = ns_names.map{|name| Resolv.getaddress(name) }
Now ip_addresses is an array of IP addresses that you can use in your template.
If you pass an array you could do:
def resolve_name(ns_name)
res = []
ns_name.each do |n|
res << {name: n, ip: Resolv.getaddress(name) }
end
res
end
And get an array of hashes so you know which address has which ip

Error when checking if an array element actually hold data

I have a short app that check if my music files are names to a specific routine (track number and then track name), but I'm getting an error whenever there are no files that need renaming, because the array in initialised, but the first item is nothing, null, empty (however VB refers to it).
To try and fix this, I'm running this check, but I'm still getting an error.
' Array declared like this
Dim nc_full_names(0) As String
<Code goes here to ReDim 'nc_full_names' and add the file name to the array, if a file needs renaming>
For i = 0 To UBound(nc_full_names)
'Checking if the array element actually has something in it like this
If Not nc_full_names Is Nothing Then
My.Computer.FileSystem.RenameFile(nc_full_names(i), nc_new_names(i))
Else
Exit For
End If
Next i
Here is the error that I am getting -
Argument cannont be nothing. Parameter name: file
Can anyone tell me the correct way to carry out this check?
I found that the answer was to check the first element in the array, as opposed the array itself. Thus, changing this...
If Not nc_full_names Is Nothing Then
...to this...
If Not nc_full_names(i) Is Nothing Then
...works just fine.
You can also start with a truly empty array:
Dim nc_full_names(-1) As String
Now nc_full_names.Length = 0.

How to assert that every item in collection is within range

I have a Groovy array that will get a set number of random Integer Values. And I want to assert that each item in the array has a value within the given range. I'm trying to use Hamcrest Matchers. So my test looks like this:
#Test
void testShouldReturnArrayOfStats(){
def results = pg.rollStats()
assertThat results, everyItem(both(greaterThan(0)).and(lessThanOrEqualTo(6)))
}
When I run the test I get an assertionError
java.lang.AssertionError: Expected: every item is (a value greater than <0> and a value less than or equal to <6>)
but: was [<6>, <3>, <5>, <4>, <3>, <2>]
I've tried some variations of this but I'm not getting a passing test. just by looking at the "But:was" portion of the error I can seen that all 6 values meet the requirements, but the test still fails.
I've not used Groovy or Hamcrest for very long so I'm sure that I'm missing something.
Thanks
Could you just use groovy?
assert results.every { it in 1..6 }

Need help on visual fortran 6.6a I want to see a 100*80 array on a text file

Hi, I am trying to get a simple problem solved and see the whole array in 100*80 FORM, but as long as I tried I failed. I can do it for 5*5 array but not for 100*80 or bigger array.
Here is my code:
PROGRAM Arr
REAL:: I,J,ITER,K
DIMENSION :: A(200,200)
A(1,1)=500
A(1,80)=300
A(100,1)=50
A(100,80)=40
READ(*,*) ITER
DO K=1,ITER
DO I=2,99
DO J=2,89
A(I,J)=(A(I+1,J)+A(I-1,J)+A(I,J+1)+A(I,J-1))/4
END DO
END DO
END DO
DO J=1,80
OPEN (UNIT=9, FILE='ARR.TXT', STATUS='REPLACE')
WRITE(9,*) (A(I,J),I=1,100)
END DO
END PROGRAM
Any comment will be helpful. Please tell me in detail.
Thanks.
Do not use the simple list directed format, but specify it directly. Try
WRITE(9,'(200F12.5,x)') (A(I,J),I=1,100)
You can play with the numbers and see the difference or see some reference Fortran Formats.
Note, that the maximum record (line) length can be a problem you will not able to overcome.

Ada and SPARK identifier `State` is either undeclared or not visible at this point

I am doing an automatic train protection on Ada with SPARK approach. This is my spec in SPARK:
package Sensors
--# own State,Pointer,State1,State2;
--# initializes State,Pointer,State1,State2;
is
type Sensor_Type is (Proceed, Caution, Danger, Undef);
subtype Sensor_Index_Type is Integer range 1..3;
procedure Write_Sensors(Value_1, Value_2, Value_3: in Sensor_Type);
--# global in out State,Pointer;
--# derives State from State,Value_1, Value_2, Value_3,Pointer &
--# Pointer from Pointer;
function Read_Sensor(Sensor_Index: in Sensor_Index_Type) return Sensor_Type;
function Read_Sensor_Majority return Sensor_Type;
end Sensors;
and this is my Ada:
package body Sensors is
type Vector is array(Sensor_Index_Type) of Sensor_Type;
State: Vector;
Pointer:Integer;
State1:Sensor_Type;
State2:Sensor_Type;
procedure Write_Sensors(Value_1, Value_2, Value_3: in Sensor_Type) is
begin
State(Pointer):=Value_1;
Pointer:= Pointer + 1;
State(Pointer):=Value_2;
Pointer:= Pointer + 1;
State(Pointer):=Value_3;
end Write_Sensors;
function Read_Sensor (Sensor_Index: in Sensor_Index_Type) return Sensor_Type
is
State1:Sensor_Type;
begin
State1:=Proceed;
if Sensor_Index=1 then
State1:=Proceed;
elsif Sensor_Index=2 then
State1:=Caution;
elsif Sensor_Index=3 then
State1:=Danger;
end if;
return State1;
end Read_Sensor;
function Read_Sensor_Majority return Sensor_Type is
State2:Sensor_Type;
begin
State2 := state(1);
return State2;
end Read_Sensor_Majority;
begin -- initialization
State:=Vector'(Sensor_Index_Type =>Proceed);
pointer:= 0;
State1:=Proceed;
State2:=Proceed;
end Sensors;
I want to know why in the function Read_Sensor_Majority I can't use the State(1) or any of the State() array values. If there is a way to use them, should I put anything in the specs of SPARK to make it happen?
The errors it's showing are:
1)Expression contains referenced to variable state which has an undefined value flow error 20
2)the variable state is nether imported nor defined flow error 32
3)the undefined initial value of state maybe used in the derivation of the function value flow error 602
You need to change the spec to read
function Read_Sensor_Majority return Sensor_Type;
--# global in State;
As I said in the comments above, I was puzzled by
State := Vector'(Sensor_Index_Type => Proceed);
but the compiler accepts it so it must be OK. And a little test shows that it has the same effect as
State := Vector'(others => Proceed);
Also pleased to report that the SPARK GPL 2011 toolset is now available for Mac OS X!
Heh. Well, those are definitely SPARK errors, rather than "garden variety" compiler errors.
It would be nice to see an actual cut-and-paste version of the errors (along with an indication of which lines they are referring to) rather than just an imperfect transcription. However, I do realise that isn't always possible for security/connectivity reasons.
It looks like all three are complaining about the flow of data through your system. Without knowing which lines they refer to, the best I can suggest is to try to manually trace your flow of data through your system to try to see what their problem is.
If I had to take a wild guess with the info I have here, I'd say it perhaps has a problem with your reading of a value from State(1) in the routine Read_Sensor_Majority, because it has no way of knowing that you've previously placed a value into that array location.
The code you have in the package's begin...end body area should take care of that, except it appears to have a compile error itself, as Simon pointed out in the comments. Perhaps if you fix that problem, SPARK will understand what is going on and quit complaining about your control flows.
If SPARK likes to spit out "I'm confused" errors on code that doesn't even get past the Ada compiler, it might be wise to make sure the Ada compiler likes the pure Ada part of your code before asking SPARK to look it over.

Resources