GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
org_graphlab_Updater.cpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 #include "org_graphlab_Updater.hpp"
24 
25 using namespace graphlab;
26 
27 //---------------------------------------------------------------
28 // proxy_updater static members
29 //---------------------------------------------------------------
30 
31 jmethodID proxy_updater::java_update = 0;
32 jmethodID proxy_updater::java_add = 0;
33 jmethodID proxy_updater::java_priority = 0;
34 jmethodID proxy_updater::java_clone = 0;
42 jmethodID proxy_updater::java_gather = 0;
43 jmethodID proxy_updater::java_merge = 0;
44 jmethodID proxy_updater::java_apply = 0;
45 jmethodID proxy_updater::java_scatter = 0;
46 
47 JNIEXPORT void JNICALL
48  Java_org_graphlab_Updater_initNative
49  (JNIEnv *env, jclass clazz){
50 
52  env->GetMethodID(clazz, "update", "(JLorg/graphlab/data/Vertex;)V");
53 
55  env->GetMethodID(clazz, "add", "(Lorg/graphlab/Updater;)V");
56 
58  env->GetMethodID(clazz, "priority", "()D");
59 
61  env->GetMethodID(clazz, "clone", "()Lorg/graphlab/Updater;");
62 
64  env->GetMethodID(clazz, "isFactorizable", "()Z");
65 
67  env->GetMethodID(clazz, "gatherEdges", "()I");
68 
70  env->GetMethodID(clazz, "scatterEdges", "()I");
71 
73  env->GetMethodID(clazz, "consistency", "()I");
74 
76  env->GetMethodID(clazz, "gatherConsistency", "()I");
77 
79  env->GetMethodID(clazz, "scatterConsistency", "()I");
80 
82  env->GetMethodID(clazz, "initGather", "()V");
83 
85  env->GetMethodID(clazz, "gather", "(Ljava/lang/Object;)V");
86 
88  env->GetMethodID(clazz, "merge", "(Lorg/graphlab/Updater;)V");
89 
91  env->GetMethodID(clazz, "apply", "(Lorg/graphlab/data/Vertex;)V");
92 
94  env->GetMethodID(clazz, "scatter",
95  "(JLjava/lang/Object;)V");
96 
97 }
98 
99 //---------------------------------------------------------------
100 // proxy_updater instance members
101 //---------------------------------------------------------------
102 
104  proxy_updater(JNIEnv *env, jobject &obj)
105  : java_any(env, obj){}
106 
108 
111 
112  // other doesn't have an existing ref
113  if (NULL == other.obj()){
114  set_obj(NULL);
115  return;
116  }
117 
118  // clone the java object
119  JNIEnv *env = core::get_jni_env();
120  set_obj(env->CallObjectMethod(other.obj(), java_clone));
121 
122 }
123 
125 
126  if (this == &other) return *this;
127  java_any::operator=(other);
128  return *this;
129 
130 }
131 
133 
134 //---------------------------------------------------------------
135 // proxy_updater instance members - the update function
136 //---------------------------------------------------------------
137 
138 void proxy_updater::operator()(icontext_type& context){
139 
140  jobject vertex = context.const_vertex_data().obj();
141  if (NULL == vertex) return; // BUG?
142 
143  // forward call to org.graphlab.Updater#update
144  JNIEnv *env = core::get_jni_env();
145  env->CallVoidMethod (obj(), java_update,
146  &context,
147  vertex);
148  handle_exception(env);
149 
150 }
151 
152 //---------------------------------------------------------------
153 // proxy_updater instance members - the add function
154 //---------------------------------------------------------------
155 
156 void proxy_updater::operator+=(const proxy_updater& other) const {
157 
158  // forward call to org.graphlab.Updater#add
159  JNIEnv *env = core::get_jni_env();
160  env->CallVoidMethod (obj(), java_add, other.obj());
161  handle_exception(env);
162 
163 }
164 
165 bool proxy_updater::is_factorizable() const {
166  JNIEnv *env = core::get_jni_env();
167  bool factorizable = env->CallBooleanMethod(obj(), java_is_factorizable);
168  handle_exception(env);
169  return factorizable;
170 }
171 
172 edge_set proxy_updater::gather_edges() const {
173  JNIEnv *env = core::get_jni_env();
174  int e = env->CallIntMethod(obj(), java_gather_edges);
175  handle_exception(env);
176  switch(e){
177  case 0: return IN_EDGES;
178  case 1: return OUT_EDGES;
179  case 2: return ALL_EDGES;
180  default: return NO_EDGES;
181  }
182 }
183 
184 edge_set proxy_updater::scatter_edges() const {
185  JNIEnv *env = core::get_jni_env();
186  int e = env->CallIntMethod(obj(), java_scatter_edges);
187  handle_exception(env);
188  switch(e){
189  case 0: return IN_EDGES;
190  case 1: return OUT_EDGES;
191  case 2: return ALL_EDGES;
192  default: return NO_EDGES;
193  }
194 }
195 
196 consistency_model proxy_updater::consistency() const {
197  JNIEnv *env = core::get_jni_env();
198  int c = env->CallIntMethod(obj(), java_consistency);
199  handle_exception(env);
200  switch(c){
201  case 0: return NULL_CONSISTENCY;
202  case 1: return VERTEX_CONSISTENCY;
203  case 2: return EDGE_CONSISTENCY;
204  case 3: return FULL_CONSISTENCY;
205  default: return DEFAULT_CONSISTENCY;
206  }
207 }
208 
209 consistency_model proxy_updater::gather_consistency() const {
210  JNIEnv *env = core::get_jni_env();
211  int c = env->CallIntMethod(obj(), java_gather_consistency);
212  handle_exception(env);
213  switch(c){
214  case 0: return NULL_CONSISTENCY;
215  case 1: return VERTEX_CONSISTENCY;
216  case 2: return EDGE_CONSISTENCY;
217  case 3: return FULL_CONSISTENCY;
218  default: return DEFAULT_CONSISTENCY;
219  }
220 }
221 
222 consistency_model proxy_updater::scatter_consistency() const {
223  JNIEnv *env = core::get_jni_env();
224  int c = env->CallIntMethod(obj(), java_scatter_consistency);
225  handle_exception(env);
226  switch(c){
227  case 0: return NULL_CONSISTENCY;
228  case 1: return VERTEX_CONSISTENCY;
229  case 2: return EDGE_CONSISTENCY;
230  case 3: return FULL_CONSISTENCY;
231  default: return DEFAULT_CONSISTENCY;
232  }
233 }
234 
235 void proxy_updater::init_gather(icontext_type& context) {
236  JNIEnv *env = core::get_jni_env();
237  env->CallVoidMethod(obj(), java_init_gather);
238  handle_exception(env);
239 }
240 
241 void proxy_updater::gather(icontext_type& context, const edge_type& edge){
242  JNIEnv *env = core::get_jni_env();
243  env->CallVoidMethod(obj(), java_gather, context.const_edge_data(edge).obj());
244  handle_exception(env);
245 }
246 
247 void proxy_updater::merge(const update_functor_type& other){
248  JNIEnv *env = core::get_jni_env();
249  env->CallVoidMethod(obj(), java_merge, other.obj());
250  handle_exception(env);
251 }
252 
253 void proxy_updater::apply(icontext_type& context){
254  jobject vertex = context.const_vertex_data().obj();
255  if (NULL == vertex) return; // BUG?
256  JNIEnv *env = core::get_jni_env();
257  env->CallVoidMethod(obj(), java_apply, vertex);
258  handle_exception(env);
259 }
260 
261 void proxy_updater::scatter(icontext_type& context, const edge_type& edge){
262  JNIEnv *env = core::get_jni_env();
263  env->CallVoidMethod(obj(), java_scatter,
264  &context, context.const_edge_data(edge).obj());
265  handle_exception(env);
266 }