Chapel Iteration - parallel-processing

Working on chapel currently and trying to iterate through an array hi of type: eltType called elements and it has elements in it.
I am trying to iterate through the whole array hi and print out each element so I'm doing:
var hi : int;
hi = elements.size;
forall i in hi
{
writeln("Index: ", 0, " Element: ", elements[i]);
}
When I try that I get this error:
Cannot iterate over values of type int(64)
Not sure how to iterate through it or why this error is happening.
Any ideas or guides? I've been looking at the Chapel API.

Your code sample has a bug, since 'hi' is an integer (storing the size of the array). You might have meant 'forall i in 1..hi' for example. Either way, here is a code listing with some common patterns for such iteration.
// Declare a 1-D array storing 10, 20, 30
// Such array literals start at index 1
var elements = [10,20,30];
// Note: elements.domain is the index set of the array;
// in this case {1..3}.
writeln("loop 1");
// iterate over range, accessing elements
for i in 1..elements.size {
writeln("Index: ", i, " Element: ", elements[i]);
}
writeln("loop 2");
// as above, but in parallel (output order will differ run to run)
forall i in 1..elements.size {
writeln("Index: ", i, " Element: ", elements[i]);
}
writeln("loop 3");
// zippered iteration to iterate over array, indexes at once
for (element,i) in zip(elements,elements.domain) {
writeln("Index: ", i, " Element: ", element);
}
writeln("loop 4");
// as above, but in parallel (output order will differ run to run)
forall (element,i) in zip(elements,elements.domain) {
writeln("Index: ", i, " Element: ", element);
}
See also
http://chapel.cray.com/docs/latest/users-guide/base/forloops.html
http://chapel.cray.com/docs/latest/users-guide/base/zip.html
http://chapel.cray.com/docs/latest/primers/ranges.html

Related

Algo: replace chars of string to find the correct word

I have a string from OCR which contains some errors.
For example "2SQ41S" in place of "250415", i have a dictionary for the possible replacements:
O/Q can be replaced by 0,
S can be replaced by 5...
I can calculate the checksum to be sure that the good word is found.
Here is the function recursive which doesn't work, it will be stopped when startPosition>=6, it's before the correct word was found:
public void CombinaisonTest()
{
string date = "2SO41S";
Dictionary<char, String[]> replaceDictionary= new Dictionary<char, String[]>()
{
{'O', new []{"Q", "0"}},
{'S', new []{"8", "5", "B"}}
};
String result = "";
var r = combinations2(date, 0, replaceDictionary);
Console.WriteLine("Date: " + date);
Console.WriteLine("R: " + r);
}
public string combinations2(string date, int startPosition, Dictionary<char, String[]> dictionary)
{
Console.WriteLine("Call function " + date + ", " + startPosition);
if (string.Join("", date).Equals("250415")) //need to calculate checksum
{
Console.WriteLine("Found: " + date);
return date;
}
if (startPosition >= date.Length)
{
Console.WriteLine("Not Found: ");
return "";
}
for (int i = startPosition; i < date.Length; i++)
{
if (dictionary.ContainsKey(date.ToCharArray()[i]))
{
foreach (var value in dictionary[date.ToCharArray()[i]])
{
return combinations2(date.Remove(i, 1).Insert(i, value), startPosition + 1, dictionary);
}
}
else
{
return combinations2(date, i + 1, dictionary);
}
}
return combinations2(date, startPosition + 1, dictionary);
}
Do you have any ideas for the corrections, please?
Thank you.
There are a couple of issues with the code. The first is that when iterating through the values in the dictionary, it returns after checking the first one, so it will only ever try and substitute Q for 0 and 8 for S. The second is that you are attempting two methods of processing the characters in the string: iterative AND recursive. You don't need to iterate over the index i with a for loop and also use recursion.
Another issue (which isn't a problem in your use case but stops the algorithm being more generic) is that the algorithm attempts to do a substitution in every case where an ambiguous character is encountered, as well as iterating over each value in the dictionary you should also consider the case where the character is left unmodified.
The function can be changed to remove the outer for loop and iterate over the values in the dictionary, test each one (recursing over the remainder of the string) and only return if a match is found. A simple way to do this is to store the result in a string and only return it if it is not the empty string (since your function returns the empty string when no match is found). If all the values in the dictionary have been tried and no match has been found, then it tries recursing without modifying the string.
public string combinations2(string date, int startPosition, Dictionary<char, String[]> dictionary)
{
Console.WriteLine("Call function " + date + ", " + startPosition);
if (string.Join("", date).Equals("250415")) //need to calculate checksum
{
Console.WriteLine("Found: " + date);
return date;
}
if (startPosition >= date.Length)
{
Console.WriteLine("Not Found: ");
return "";
}
if (dictionary.ContainsKey(date.ToCharArray()[startPosition]))
{
foreach (var value in dictionary[date.ToCharArray()[startPosition]])
{
string result = combinations2(date.Remove(startPosition, 1).Insert(startPosition, value), startPosition + 1, dictionary);
if(result != "")
return result;
}
}
return combinations2(date, startPosition + 1, dictionary);
}

panic: runtime error: index out of range processing file

I'm trying to make a function that reads a collection of data from JSON file, but I keep receiving this error
panic: runtime error: index out of range
and it's not importing all the data from json file,
I think there's something wrong in my for loop.
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// initialize Int array
var interns Int
// initialize Com array
var companies Com
// unmarshal byteArray
json.Unmarshal(byteValue, &interns)
json.Unmarshal(byteValue, &companies)
// iterate through every student within our students array and
// print out the student ID, first & last name
for i := 0; i < len(interns.Int); i++ {
fmt.Println("Student first name: " + interns.Int[i].Name.FirstName)
fmt.Println("Student last name: " + interns.Int[i].Name.LastName)
fmt.Println("Company name: " + companies.Com[i].CompanyName)
fmt.Println("Company email address: " + companies.Com[i].CompanyEmail + "\n")
}
Because you are using len of Int array to iterate Com array while len of Com array is different so the program will panic when iterate to an index that Com array is not available
//Program panics if [i] is not valid
fmt.Println("Company name: " + companies.Com[i].CompanyName)
fmt.Println("Company email address: " + companies.Com[i].CompanyEmail + "\n")
Split the loop into two to iterate and it will works
// iterate Int array
for i := 0; i < len(interns.Int); i++ {
fmt.Println("Student first name: " + interns.Int[i].Name.FirstName)
fmt.Println("Student last name: " + interns.Int[i].Name.LastName)
}
// iterate Com array
for i := 0; i < len(companies.Com); i++ {
fmt.Println("Company name: " + companies.Com[i].CompanyName)
fmt.Println("Company email address: " + companies.Com[i].CompanyEmail + "\n")
}

Using .sum() and += on std::valarray<T>

I am using the type std::valarray<std::valarray<double>> and wish to sum each of the contained valarrays element wise, to leave a std::valarray<double>.
The C++ documentation states that the operator .sum() can be applied to std::valarray<T> so long as the operator += is defined for type T. My code below (method1) tries to apply this to std::valarray<std::valarray<double>>, but the result appears to be nonsense.
However if I perform this manually, using the += operator (method2), I get the result I want. But the fact that method2 works seems to imply that the operator += is defined for the type std::valarray<double>, and hence that method1, using .sum(). should work. I really can't understand what is happening here...
My code:
#include <iostream>
#include <valarray>
// Attempt to use .sum() operator
std::valarray<double> method1(const std::valarray<std::valarray<double>>& data) {
return data.sum();
}
// Manual summation using += operator
std::valarray<double> method2(const std::valarray<std::valarray<double>>& data) {
std::valarray<double> sum(data[0].size());
for (size_t i{0}; i < data.size(); i++) {
sum += data[i];
}
return sum;
}
// Display size and elements
void showData(const std::valarray<double> data) {
std::cout << "Size = " << data.size() << "\n";
std::cout << "Data = ";
for (size_t i{0}; i < data.size(); i++) {
std::cout << data[i] << " ";
}
std::cout << "\n\n";
}
int main() {
std::valarray<std::valarray<double>> data{{1,2},{3,4}};
showData(method1(data));
showData(method2(data));
}
My output:
Size = 0
Data =
Size = 2
Data = 4 6
The sum method of std::valarray requires operator+= to be defined for its value type (in your case, std::valarray), but std::valarray also requires it to be default-constructible (from the "Numeric" concept requirement).
This allows the sum method to work without operator+, by first default-constructing an element, and then adding each contained element with operator+=.
Although it isn't defined anywhere, as far as I know, it probably works something like this.
T sum() const {
T result;
for (auto& it : elements) {
result += it;
}
return result;
}
The problem with a valarray of valarrays (std::valarray<std::valarray>) is that a default-constructed valarray is empty. And when operator+= is applied with an empty valarray and a non-empty one, it results in undefined behavior ("The behavior is undefined if size() != v.size()"). What you are likely to get is an empty valarray as a result (but you could potentially get anything).
What you could use instead is std::accumulate. It requires an initial value as third parameter, which takes care of the problem.
std::accumulate(std::begin(data), std::end(data), std::valarray<double>(data[0].size()))
Live on Coliru.
PS: don't ask me why std::valarray has no method begin and end.

Distance edit array output

I am doing an edit distance with the user input. I am storing my values in array. then the edit distance will compare the user input with my array of strings. I am doing a loop that if the edit distance is more than 2 it will display invalid else valid.
The only problem I've got is that although the program is working out fine, the output is the result of all the '28' strings that I have in my array. I would like to display only invalid or valid once.
Test is my array of strings and user is - String user - the user input.
void testingLD()
{
for (int i=0; i<test.length; i++)
{
if(getLevenshteinDistance(test[i],user) > 2)
{
println ("Invalid re-input");
}
else
{
println ("Valid");
}
}
}
You have your print line functions inside your loop so they get printed once per iteration.
Try this.
void testingLD()
{
boolean isValid = true; // assume true, check each and update
// begin loop
for (int i=0; i<test.length; i++)
{
if(getLevenshteinDistance(test[i],user) > 2)
{
isValid = false;
break; // don't need to test the rest of the loop if we already found a false
}
}
// end loop
if(isValid){
println("Valid");
}else{
println("Invalid re-input");
}
}
Similarly you could count the number of valid int validCount = 0; validCount++ and then display stats about how many were valid, the percentage etc. Or keep an array of the invalid strings and display those as the ones that fail etc!
Wrap up:
When you want to check an entire collection or array for some condition and output one answer make sure to have your output outside of the loop!

How understand data() method of boost::array, and return add length?

boost::array<char,7> buf = {'a','b','c','d','e','f','g'};
...
...
std::cout << buf.data() + 5;
It's display: fg
How to understand it?
buf.data() + 5
Thanks
buf.data() seems to return a pointer to the internal array buffer in question.
From there, standard pointer arithmetic applies, and you see the 6th character onwards in the std::cout.operator<< call.
buf.data() is defined to return a pointer to the first element of the array, and the elements in a Boost.Array are defined to be contiguous.
So buf.data() + 5 will be a pointer to the element (in this case, character) of the array.
You could also write &buf[5] and get the same pointer.
Note that in the code above:
std::cout << buf.data() + 5;
you are attempting to print the value of the pointer, not the character it points to.

Resources