X10 x10.lang.FailedDynamicCheckException: !(here == x$0.home) C++ Backend - exception

i'm playing around with X10 currently, and i'm getting multiple exceptions. This is the code i created:
Main.x10
public class Main {
// main Method for the Main class
public static def main(argv:Rail[String]) {
Console.OUT.println("This is the main place. (ID="+here.id+")");
val dataItem = new Data();
// no global reference, data is copied to each place.
finish for (p in Place.places()) {
at (p) async {
Console.OUT.println("This is place no: "+here.id+". The value of dataItem is "+dataItem.getValue()+".");
dataItem.incValue();
}
}
Console.OUT.println("Final value of dataItem without GlobalRef in multiple places at place "+here.id+": "+dataItem.getValue());
dataItem.setValue(0);
finish for (p in Place.places()) {
async {
Console.OUT.println("This is place no: "+here.id+". The value of dataItem is "+dataItem.getValue()+".");
dataItem.incValue();
}
}
Console.OUT.println("Final value of dataItem without GlobalRef in one place at place "+here.id+": "+dataItem.getValue());
dataItem.setValue(0);
val globalRefDataItem = GlobalRef[Data](dataItem);
finish for (p in Place.places()) {
at (p) async {
val globalDataItemUnwrapped = globalRefDataItem();
Console.OUT.println("This is place no: "+here.id+". The value of dataItem is "+globalDataItemUnwrapped.getValue()+".");
atomic globalDataItemUnwrapped.incValue();
}
}
Console.OUT.println("Final value of dataItem with GlobalRef in multiple places at place "+here.id+": "+dataItem.getValue());
}
}
Data.x10
public class Data{
private var value:Long;
def getValue():Long{
return value;
}
def setValue(value:long){
this.value = value;
}
def incValue(){
this.value = this.value+1;
}
}
The output in X10DT is the following:
This is the main place. (ID=0)
This is place no: 3. The value of dataItem is 0.
This is place no: 1. The value of dataItem is 0.
This is place no: 2. The value of dataItem is 0.
This is place no: 0. The value of dataItem is 0.
Final value of dataItem without GlobalRef in multiple places at place 0: 0
This is place no: 0. The value of dataItem is 0.
This is place no: 0. The value of dataItem is 1.
This is place no: 0. The value of dataItem is 2.
This is place no: 0. The value of dataItem is 3.
Final value of dataItem without GlobalRef in one place at place 0: 4
This is place no: 0. The value of dataItem is 0.
Command used: /home/martze/x10dt/workspace/Example/bin/Main
Uncaught exception at place 0: x10.lang.MultipleExceptions
x10.lang.FailedDynamicCheckException: !(here == x$0.home)
at x10aux::throwException(x10::lang::CheckedThrowable*)
at Main__closure__3::__apply()
at x10::lang::Activity::run()
at x10::lang::Runtime__Worker::loop()
at x10::lang::Runtime__Worker::__apply()
at x10::lang::Runtime__Pool::run()
at
at GC_inner_start_routine
at GC_call_with_stack_base
at
at clone
x10.lang.FailedDynamicCheckException: !(here == x$0.home)
at x10aux::throwException(x10::lang::CheckedThrowable*)
at Main__closure__3::__apply()
at x10::lang::Activity::run()
at x10::lang::Runtime__Worker::loop()
at x10::lang::Runtime__Worker::__apply()
at x10::lang::Runtime__Pool::run()
at
at GC_inner_start_routine
at GC_call_with_stack_base
at
at clone
x10.lang.FailedDynamicCheckException: !(here == x$0.home)
at x10aux::throwException(x10::lang::CheckedThrowable*)
at Main__closure__3::__apply()
at x10::lang::Activity::run()
at x10::lang::Runtime__Worker::loop()
at x10::lang::Runtime__Worker::__apply()
at x10::lang::Runtime__Pool::run()
at
at GC_inner_start_routine
at GC_call_with_stack_base
at
at clone
I tried googling, but the results were only pointing to some documentation that didn't help me at all. My environment variables are set to X10_NTHREADS=1 and i set the number of places to 4. I'm working with Ubuntu and i'm using the C++ Backend.
What can i do about this error and what does this error mean?

A GlobalRef is a globally meaningful reference to an object at a particular place. It may only be dereferenced at the place where it was created (the "home" place). The exception is thrown at the dereferencing ('unwrapping') of the GlobalRef:
val globalRefDataItem = GlobalRef[Data](dataItem);
finish for (p in Place.places()) {
at (p) async {
val globalDataItemUnwrapped = globalRefDataItem();
...
}
}
The GlobalRef was created at place 0, but is being dereferenced (globalRefDataItem()) at place p. The error would be easier to understand if it read:
x10.lang.FailedDynamicCheckException: (here != globalRefDataItem.home)
To access the object that is referred to by the GlobalRef, the activity must change place back to the home place. For example:
val globalRefDataItem = GlobalRef[Data](dataItem);
finish for (p in Place.places()) {
at (p) async {
at(globalRefDataItem.home) {
atomic globalDataItemUnwrapped.incValue();
}
}
}
This is often referred to as the "ping-pong" idiom - the activity changes to a remote place and then changes back again to the original place.
In your program, there is the added requirement to print the value of dataItem before it was incremented. To do this, use an at expression (X10 language spec ยง13.3) to return a value from the remote place, for example:
finish for (p in Place.places()) {
at (p) async {
val currentValue = at(globalRefDataItem.home) {
val globalDataItemUnwrapped = globalRefDataItem();
var valueBeforeInc:Long = 0;
atomic {
valueBeforeInc = globalDataItemUnwrapped.getValue();
globalDataItemUnwrapped.incValue();
}
valueBeforeInc
};
Console.OUT.println("This is place no: "+here.id+". The value of dataItem is "+currentValue+".");
}
}
Note, I made an important change to the original code, to combine the get and increment of the value in a single atomic block. Without this change, it would be possible for another activity to increment the value in between these two statements. X10 provides atomic types (e.g. x10.util.concurrent.AtomicLong) which provide just such an atomic getAndIncrement operation.

Related

How to specify a return of an array of unknown size in Chapel

I tried to rely on type inference for a function with signature:
proc mode(data: [?]int)
but the compiler said it could not resolve the return type (which is a warning in in itself I guess given there are only two return statements). I tried:
proc mode(data: [?]int): [?]int
but the compiler then said there was an internal error:
internal error: CAL0057 chpl Version 1.13.1.518d486
What is the correct way of specifying that the length of an array returned by a function can only be known at run time?
If the domain/size of the array being returned cannot be described directly in the function prototype, I believe your best bet at present is to omit any description of the return type and lean on Chapel's type inference machinery to determine that you're returning an array (as you attempted). For instance, here is a procedure that reads in an array of previously unknown size and returns it:
proc readArrFromConsole() {
var len = stdin.read(int);
var X: [1..len] real;
for x in X do
x = stdin.read(real);
return X;
}
var A = readArrFromConsole();
writeln(A);
Running it and typing this at the console:
3 1.2 3.4 5.6
Generates:
1.2 3.4 5.6
Your question mentions multiple return statements, which opens up the question about how aggressively Chapel unifies types across distinct arrays. A simple example with multiple arrays of the same type (each with a unique domain, size, and bounds) seems to work:
proc createArr() {
var len = stdin.read(int);
if (len > 0) {
var X: [1..len] real;
return X;
} else {
var Y: [-1..1] real;
return Y;
}
}
var A = createArr();
writeln(A);
To understand why the compiler couldn't resolve the return type in your example may require more information about what your procedure body / return statements contained.
I've come across this from time to time in recursive functions, in situations where omitting the return type fails; in this case I create a record which is an array with its domain, e.g.:
record stringarray {
var D: domain(1);
var strs : [D] string;
}
and then define the recursive array to return one of those records:
proc repeats() : stringarray {
var reps: stringarray;
//...
for child in children do {
childreps = child.repeats();
for childrep in childreps do
reps.push_back(childrep);
}
//...
return reps;
}

error (Reducer: ) when attempting to do distinct reduce

I am getting an error when trying to do a DISTINCT reduce that I got from here. I have reproduced this error on the beer-sample bucket, so this should be easy to reproduce. I have not seen any errors in the mapreduce_errors.txt file, or anything that would lead me anywhere in the others. (If you would like me to search or post snippets of other files, please ask).
Running couchbase enterprise 4 beta, on Windows 2008 R2 (This also happened on the 3.0.1 community edition as well.).
Here is my map function (Using the beer-sample bucket, that ships directly with couchbase).
function(doc, meta) {
switch(doc.type) {
case "brewery":
emit(meta.id);
break;
}
}
Here is my reduce function:
function(keys, values, rereduce) {
return keys.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
This is the error:
reason: error (Reducer: )
Also an imgur of the view page if it helps: http://i.imgur.com/KyLutMc.png
The problem lies within your custom reduce function: you're not handling the case when it's being called as part of a re-reduce.
As per Couchbase documentation:
The base format of the reduce() function is as follows:
function(key, values, rereduce) {
...
return retval;
}
The reduce function is supplied three arguments:
key: The key is the unique key derived from the map() function and the
group_level parameter.
values: The values argument is an array of all of the values that match
a particular key. For example, if the same key is output three times,
data will be an array of three items containing, with each item
containing the value output by the emit() function.
rereduce: The rereduce indicates whether the function is being called
as part of a re-reduce, that is, the reduce function being called
again to further reduce the input data.
When rereduce is false:
The supplied key argument will be an array where the first argument is the key as emitted by the map function, and the id is the document ID that generated the key.
The values is an array of values where each element of the array matches the corresponding element within the array of keys.
When rereduce is true:
key will be null.
values will be an array of values as returned by a previous reduce() function. The function should return the reduced version
of the information by calling the return() function. The format of the
return value should match the format required for the specified key.
Bold formatting is mine, and the highlighted words are quite important: you should consider that sometimes, you'll receive the keys argument with a value of null.
According to the docs, you should handle the case when rereduce is true within your reduce() function, and you should know that in this case, keys will be null. In the case of your reduce() function, you could do something like this:
function(keys, values, rereduce) {
if (rereduce) {
var result = [];
for (var i = 0; i < values.length; i++) {
var distinct = values[i];
for (var j = 0; j < distinct.length; j++) {
result.push(distinct[j]);
}
}
return result.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
return keys.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});
}
Here, I'm firstly handling the re-reduce phase. For this I'm flattening the array of arrays that I'm receiving in the values argument and then I'm removing the duplicates that might have appeared after the merge.
Then it comes your original code, which returns the keys argument array without duplicates.
To test that this reduce() function actually works, I've used the following map() function:
function(doc, meta) {
switch(doc.type) {
case "brewery":
emit(meta.id, null);
emit(meta.id, null);
break;
}
}
This intentionally generates duplicates, which then are removed by the reduce() function.
While this reduce works as a development view, it does not as a production view. The dataset must be too large so you have to implement the rereduce. This documentation should help http://docs.couchbase.com/admin/admin/Views/views-writing.html#reduce-functions

Scala Saving Sort Function Parameters

I'm trying to save the parameters used to sort a sequence in Scala for deferred execution at a later time.
For example, instead of "list.sortBy (.value)", I want to save the (".value") sort function, and retrieve this sort function ("_.value") at a later time for the actual sorting.
How do I save and retrieve the sort function arguments for deferred execution? Here is some sample test code:
class SortTest {
def testSort () = {
val myClass = new MyClass(0)
val list = List (myClass, new MyClass(1), new MyClass(2), new MyClass(3), new MyClass(4))
// Want to sort by value attribute, but don't want to sort right away. Rather
// how do I save the sort function, and retrieve it at a later time for execution?
list.sortBy(_.value)
// save the sort function (i.e. sort by the value attribute of myClass)
// something similar to the following syntax
myClass.setSortFunction (_.value)
// retrieve the sort function and sort the list
list.sortBy(myClass.getSortFunction())
}
class MyClass (d:Int){
val value = d
val sortFunc = null
// what should be the signature of this function ?
def setSortFunction (sortFunc: ()) = {
this.sortFunc = sortFunc
}
// what should be the return type of this function?
def getSortFunction () = {
return sortFunc
}
}
}
You could do something like this:
val sortFunction = (x : { def value: Int } ) => x.value
At this point, you might not be happy with the hardcoding of Int. Unfortunately, a function must have well defined types, so I cannot make this generic on the return type.
One could instead make it a definition:
def sortFunction[T] = (x : { def value: T } ) => x.value
However, you cannot pass definitions around, only values, and values cannot be parameterized.
On the other hand, you are approaching this the wrong way -- there's an assumption there that sortBy takes a function as a parameter, and only that. Not true: sortBy takes two parameters: a function, and an Ordering. If you don't save the ordering, you cannot sort it.
And here we get to the other problem... the function must have a type MyClass => T, and the ordering must be of type Ordering[T]. Without knowing in advance what T is, you cannot save that.
Fortunately, and the reason why Ordering is a good idea, you can simply create an Ordering[MyClass], and use that!
Here's how:
class MyClass(d: Int) {
val value = d
private var sortFunction: Ordering[MyClass] = _
def setSortFunction[T : Ordering](f: MyClass => T) {
sortFunction = Ordering by f
}
def getSortFunction = sortFunction
}
And you use it like this:
list.sorted(myClass.getSortFunction)
Notice that instead of sortBy it uses sorted. The method sortBy is implemented by creating an Ordering and calling sorted with it, so you are not losing any performance.

Sort an Array of MovieClips on values within their child clips

I have a class called Table that is linked to a clip on the stage.
During setup I create many Table instances and pass each one an instance of a TableData class. This TableData object includes an ID value.
I put a selection of my Table objects in an Array which I would lik eto sort based on the ID value within each Table's instance of TableData.
What I had hoped to use was something like: myArray.sortOn("tableData.id");
This doesn't seem to work and I assume that Array.sortOn() can't drill down into child clips.
Is there a way that I can achieve this?
The Array and generic Vector.<> collections both provide an alternate form of the .sort() method which takes a function. You can provide your own sorting implementation, in this case which peeks objects and compares only specific pieces of them.
Consider the following class:
class TableData {
public var ID:String;
public function TableData(id:String):void {
this.ID = id;
}
}
This is a simplified version of your TableData class that only provides a ID. Then a simplified Table class:
class Table {
private var _data:TableData;
public function get data():TableData {
return _data;
}
public function Table(tableData:TableData) {
_data = tableData;
}
}
Finally, lets put them to use, and spin up a new collection.
var list:Vector.<Table> = new Vector.<Table>();
list.push(new Table(new TableData("X")));
list.push(new Table(new TableData("C")));
list.push(new Table(new TableData("S")));
list.push(new Table(new TableData("A")));
Now, we need to make a new function that will actually do our comparison:
private function sortUsingDataID(a:Table, b:Table):int {
var aID:String = a.data.ID;
var bID:String = b.data.ID;
return aID < bID ? aID == bID ? 0 : -1 : 1;
}
This function will expect to get two items, and should return either -1, 0, or 1. If a should come before b then the function should return -1. Otherwise, if a should come after b, then it should return 1. Lastly, if the two are the same, then it should return a value of 0.
So, now we just need to tell the array to resort itself using our custom sorting function by calling .sort and passing in our function.
list.sort(sortUsingDataID);
Now the list should be sorted the way that you want.
See an working example here.
See the documentation for more details

The easiest way to check if all values of std::map are equal

What is the easiest way to check if values of some std::map are equal for all the keys, without (at least, visible) iterating over all of them? Can it be done in one operation?
Get the value of the first element and then check the remaining ones using std::all_of with a custom predicate. Something like:
if (!mp.empty()) {
int val = mp.begin()->second;
result = std::all_of(std::next(mp.begin()), mp.end(),
[val](typename <insert map type>::const_reference t){ return t->second == val; });
}
Use std::unique and then verify that the distance between the begin iterator of the map and the end iterator returned by std::unique is 1.
This function may suit your needs
template <typename Map>
bool IsUnique(const Map& i_map)
{
return std::count_if(i_map.cbegin(), i_map.cend(),
[&i_map] (typename Map::const_reference v)
{
return v.second == i_map.cbegin()->second;
}) == i_map.size();
}
You can even do it without the if statement checking for an empty map with std::all_of if you can live with one redundant comparison:
template<typename Key, typename Value>
bool all_equal(std::map<Key, Value> const& map)
{
// the lambda will only get called when the map is not empty
// so we can safely access begin()->second
auto const cmpWithFirst = [&](std::pair<Key,Value> const& i)
{
return map.begin()->second == i->second;
};
return std::all_of(map.begin(), map.end(), cmpWithFirst);
}
This compares all elements to the first element if there are any, including the comparison of the first one against the first element.