I am trying to implement a graph data structure in the D language which supports parallel iteration over the node and edge sets.
alias ulong index;
alias index node;
alias ulong count;
class Graph {
index z; // max node index
count n; // number of nodes
count m; // number of edges
node[][] adja; // adjacency list
count[] deg; // node degree
this(count n = 0) {
this.z = n;
this.n = n;
this.m = 0;
this.adja = new node[][](this.z, 0);
this.deg = new count[](this.z);
}
Here's a sequential node iterator method:
/**
* Iterate over all nodes of the graph and call handler (lambda closure).
*/
void forNodes(F)(F handle) {
foreach (node v; 0 .. z) {
// call here
handle(v);
}
}
Works like this, and seems to work fine:
ulong sum1 = 0;
G.forNodes((node v) {
sum1 += v;
});
Now I try a parallel version using the 'std.parallelism' module:
void parallelForNodes(F)(F handle) {
foreach (node v; taskPool.parallel(z)) {
// call here
handle(v);
}
}
But this gives me the a compiler error. What am I doing wrong here?
cls ~/workspace/Prototypes/PLPd $ ./main.d
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3795): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(3796): Error: cannot have parameter of type void
/usr/local/Cellar/dmd/2.063/src/phobos/std/parallelism.d(1539): Error: template instance std.parallelism.ParallelForeach!(ulong) error instantiating
Graph.d(90): instantiated from here: parallel!(ulong)
./main.d(100): instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow #safe)
Graph.d(90): Error: template instance std.parallelism.TaskPool.parallel!(ulong) error instantiating
./main.d(100): instantiated from here: parallelForNodes!(void delegate(ulong v) nothrow #safe)
./main.d(100): Error: template instance Graph.Graph.parallelForNodes!(void delegate(ulong v) nothrow #safe) error instantiating
Failed: 'dmd' '-v' '-o-' './main.d' '-I.'
parallel takes a range. Use std.range.iota to get the range equivalent of 0 .. z: foreach (v; parallel(iota(z))) {...}
Related
I want to use the nlohmann/json library to serialize some json to a QObject and vice versa.
The problem I run into is that the parser is trying to use the copy constructor of the QObject, wish is not allowed.
There is something in the documentation regarding this https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types but I can't manage to make it work.
template <>
struct adl_serializer<MyQObject>
{
static MyQObject from_json(const json& j)
{
return {j.get<MyQObject>()}; // call to implicitly-deleted copy constructor of 'MyQObject'
}
static void to_json(json& j, MyQObject t)
{
j = t; // no viable overload '='
}
};
inline void from_json(const json& j, MyQObject& x)
{
x.setXxx(j.at("xxx").get<typeOfXxx>()):
// ...
}
inline void to_json(json& j, const MyQObject& x)
{
j = json::object();
j["xxx"] = x.xxx();
// ...
}
What should I write in the adl_serializer to make it work ?
I face this problem many times when i implement the Graph data-structure in c++14 via vector, I search a lot but it could not help me because it gives me search results about array though i want vector's solution. Please help me to solve this Error.
I got an error in -> positions define below,
for(ll i=0;i<m;i++)
{
cin>>x>>y;
-> v[x].push_back(y);
-> v[y].push_back(x);
}
#include<bits/stdc++.h>
using namespace std;
#define ll int
vector<ll> v[300005];
bool vis[300005];
vector<ll> vv[300005];
void dfs(int s,int p);
int main()
{
ll n,m,u,v;
cin>>n>>u>>v;
ll x,y;
m=n-1;
for(ll i=0;i<m;i++)
{
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
for(ll i=1;i<=n;i++)
{
memset(vis,0,vis[0]*n);
dfs(i,i);
}
ll ans=0;
ll o=(n*(n-1))/2;
ll p=0;bool fu=0,fv=0;
for(ll i=0;i<n;i++)
{
p=0;fu=0;fv=0;
for(ll j=0;j<n;j++)
{
if(fu && vv[i][j]==v){
fv=1;break;
}
if(vv[i][j]==u) fu=1;
if(fu) p++;
}
ans+=o-(n-p);
}
cout<<ans;
return 0;
}
void dfs(int s,int p)
{
vis[s]=1;vv[p].push_back(s);
for(ll i=0;i<v[s].size();i++)
{
if(vis[v[s][i]]==0) //Very IMP - vis[v[s][i]]
dfs(v[s][i],p);
}
}
I got an error as :
29:12: error: invalid types 'int[int]' for array subscript
v[x].push_back(y);
30:12: error: invalid types 'int[int]' for array subscript
v[y].push_back(x);
But i Expect the result correctly, because i trace it and i could not find any errors in my code. Please Give Solution.
You have declared two variables of type v; one inside main of type int and second outside main of type vector. The code inside the for loop is referring to v of type int. Either change the name of either of the two variables or use scope resolution operator :: to access the global variable.
for(ll i=0;i<m;i++)
{
cin>>x>>y;
::v[x].push_back(y);
::v[y].push_back(x);
}
I need pass returnValue to a method as argument passed by reference and adjust original var value when function id done. So using ReferenceArgumentHelper class.
What's wrong in code bellow when returnValue is unintentionally deleted (when it is a node, i.e. string) and valgrind detects it. callMethod("onFunctionExit" calls an Qore script method and I can see there correct returnValue value. I suspect it's deleted when exiting onFunctionExit when ReferenceArgumentHelper is destroyed. rah.getArg() references reference variable, so it should not be deleted in callMethod.
DLLLOCAL ThreadDebugEnum callMethod(const char* name, const ThreadDebugEnum defaultResult, QoreProgram *pgm, int paramCount, AbstractQoreNode** params, ExceptionSink* xsink) {
int rv;
QoreListNode* l = new QoreListNode();
qore_program_to_object_map_t::iterator i = qore_program_to_object_map.find(pgm);
if (i == qore_program_to_object_map.end()) {
return defaultResult;
}
i->second->ref();
l->push(i->second);
for (int i=0; i<paramCount; i++) {
if (params[i])
params[i]->ref();
l->push(params[i]);
}
rv = qo->intEvalMethod(name, l, xsink);
l->deref(xsink);
return (ThreadDebugEnum) rv;
}
DLLLOCAL virtual ThreadDebugEnum onFunctionExit(QoreProgram *pgm, const StatementBlock *blockStatement, QoreValue& returnValue, ExceptionSink* xsink) {
AbstractQoreNode* params[2];
params[0] = getLocation(blockStatement);
ReferenceArgumentHelper rah(returnValue.takeNode(), xsink); // grab node from returnValue and pass to helper
params[1] = rah.getArg(); // caller owns ref
ThreadDebugEnum rv = callMethod("onFunctionExit", DBG_SB_RUN, pgm, 2, params, xsink);
AbstractQoreNode* rc = rah.getOutputValue(); // caller owns ref
returnValue.assign(rc); // takes reference
// returnValue.ref();
}
return rv;
}
When looking deeply I did not get why compiler is happy with code in /lib/ReferenceArgumentHelper.cpp:
struct lvih_intern {
LocalVar lv;
ExceptionSink* xsink;
ReferenceNode* ref;
DLLLOCAL lvih_intern(AbstractQoreNode* val, ExceptionSink* xs) : lv("ref_arg_helper", 0), xsink(xs) {
printd(5, "ReferenceArgumentHelper::ReferenceArgumentHelper() instantiating %p (val: %p type: '%s') \n", &lv, val, val ? val->getTypeName() : "n/a");
lv.instantiate(val); <--------------
VarRefNode* vr = new VarRefNode(strdup("ref_arg_helper"), VT_LOCAL);
vr->ref.id = &lv;
ref = new ReferenceNode(vr, 0, vr, 0);
}
class LocalVar {
....
DLLLOCAL void instantiate(QoreValue nval) const {
What is behind conversion AbstractQoreNode* to QoreValue in method call? I did not find an overloaded operator or so. I'm looking what exactly happens with references.
** EDIT **
To make a long story short, ReferenceArgumentHelper was buggy; it hadn't been used in years and was not up to date. The class has been fixed which should fix your issue I hope.
Thank you for pointing this out, and let me know if you have any further problems with this or the fix to the affected code.
I am trying to load a java class to oracle as a function. In the server I managed to use loadjava as below:
C:\Users\n12017>loadjava -user USER1/passw E:\JAVA_repository\SOOSProjects\Mehmet_java_2db_trial\classes\mehmet_java_2db_trial\kondrakk.class
And in the oracle db side:
create or replace function ngram_kondrakk(src in varchar2, trg in varchar2)
return float
as language java
name 'mehmet_java_2db_trial/kondrakk.getDistance(java.lang.string, java.lang.string) return java.lang.float';
/
However, when I apply the query as below, I got error. (As a result of the query I am expecting a similarity score of 1 since two identical strings are compared)
select ngram_kondrakk('mehmet','mehmet') from dual;
Here is the error:
ORA-29532: Java call terminated by uncaught Java exception: System error : java/lang/UnsupportedClassVersionError
29532. 00000 - "Java call terminated by uncaught Java exception: %s"
*Cause: A Java exception or error was signaled and could not be
resolved by the Java code.
*Action: Modify Java code, if this behavior is not intended.
Finally, here is the code that I am trying to use:
package mehmet_java_2db_trial;
public class kondrakk {
public static float getDistance(String source, String target) {
final int sl = source.length();
final int tl = target.length();
if (sl == 0 || tl == 0) {
if (sl == tl) {
return 1;
}
else {
return 0;
}
}
int n=3;
int cost = 0;
if (sl < n || tl < n) {
for (int i=0,ni=Math.min(sl,tl);i<ni;i++) {
if (source.charAt(i) == target.charAt(i)) {
cost++;
}
}
return (float) cost/Math.max(sl, tl);
}
char[] sa = new char[sl+n-1];
float p[]; //'previous' cost array, horizontally
float d[]; // cost array, horizontally
float _d[]; //placeholder to assist in swapping p and d
//construct sa with prefix
for (int i=0;i<sa.length;i++) {
if (i < n-1) {
sa[i]=0; //add prefix
}
else {
sa[i] = source.charAt(i-n+1);
}
}
p = new float[sl+1];
d = new float[sl+1];
// indexes into strings s and t
int i; // iterates through source
int j; // iterates through target
char[] t_j = new char[n]; // jth n-gram of t
for (i = 0; i<=sl; i++) {
p[i] = i;
}
for (j = 1; j<=tl; j++) {
//construct t_j n-gram
if (j < n) {
for (int ti=0;ti<n-j;ti++) {
t_j[ti]=0; //add prefix
}
for (int ti=n-j;ti<n;ti++) {
t_j[ti]=target.charAt(ti-(n-j));
}
}
else {
t_j = target.substring(j-n, j).toCharArray();
}
d[0] = j;
for (i=1; i<=sl; i++) {
cost = 0;
int tn=n;
//compare sa to t_j
for (int ni=0;ni<n;ni++) {
if (sa[i-1+ni] != t_j[ni]) {
cost++;
}
else if (sa[i-1+ni] == 0) { //discount matches on prefix
tn--;
}
}
float ec = (float) cost/tn;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+ec);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
System.out.println(1.0f - (p[sl] / Math.max(tl, sl)));
return 1.0f - (p[sl] / Math.max(tl, sl));
}
}
Please HELP!
Thanks in advance...
When compiling Java classes to load into an Oracle database, be sure that you compile your Java classes to run with the JVM inside the Oracle database.
The version of Java that comes with an Oracle database is typically out of date compared to the current Java: expect to be using Java 1.4 with Oracle 10g and 1.5 with 11g. Your best bet is to use the Java compiler that ships with the database, but if you can't do that, use -target 1.5 or suchlike to force the compiler to compile classes to run on Java 1.5.
Your plsql wrapper function has "/"
...mehmet_java_2db_trial/kondrakk.getDistance...
replace / with . [dot]
Check the docs
and as it was already mentioned - sync JVM of compilation with JVM for run-time( which will be Oracle JVM that is "attached" to DB)
I'm in need of the Collections object but Processing.js keeps spitting back an error saying Collections is not defined as though it doesn't recognize it as an object. I'm trying to find the minimum value of an ArrayList by using the Collections.min function so this would be really useful.
ArrayList<int> aaa = new ArrayList<int> ();
println(aaa);
Collections<int> fff = new Collections<int> ();
println(fff);
The Collections object is not a Processing API object, but an underlying Java object, and is not available to all interpreters of Processing code (because not all interpreters are based on the JVM).
If you want to find the minimum value, it's three lines of code:
int minval = aaa.get(0);
for(int v: aaa) {
if(v < minval) { minval = v; }
}
Done, we have our minimum value. If we wrap this in a function, we can use it wherever we want:
int getMinValue(ArrayList<Integer> numberlist) {
int minval = numberlist.get(0);
for(int v: numberlist) {
if(v < minval) { minval = v; }
}
return minval;
}