1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <jni.h>
18
19 #include "example_iterator_wrapper_impl.h"
20 #include "fcp/client/fcp_runner.h"
21 #include "fcp/client/fl_runner.pb.h"
22 #include "fcp/client/interruptible_runner.h"
23 #include "fcp/jni/jni_util.h"
24 #include "flags_impl.h"
25 #include "log_manager_wrapper_impl.h"
26 #include "more_jni_util.h"
27 #include "simple_task_environment_wrapper_impl.h"
28
29 #define JFUN(METHOD_NAME) \
30 Java_com_android_federatedcompute_services_training_jni_FlRunnerWrapper_##METHOD_NAME // NOLINT
31
32 using fcp::jni::ParseProtoFromJByteArray;
33
JFUN(runNativeFederatedComputation)34 extern "C" JNIEXPORT jbyteArray JNICALL JFUN(runNativeFederatedComputation)(
35 JNIEnv *env, jclass, jobject java_simple_task_env,
36 jstring population_name_jstring, jstring session_name_jstring,
37 jstring task_name_jstring, jobject java_native_log_manager,
38 jbyteArray client_only_plan_bytes,
39 jstring checkpoint_input_filename_jstring,
40 jstring checkpoint_output_filename_jstring) {
41 google::internal::federated::plan::ClientOnlyPlan client_only_plan =
42 ParseProtoFromJByteArray<
43 google::internal::federated::plan::ClientOnlyPlan>(
44 env, client_only_plan_bytes);
45
46 const fcp::client::engine::jni::FlagsImpl flags;
47 JavaVM *jvm = MoreJniUtil::getJavaVm(env);
48 fcp::client::engine::jni::SimpleTaskEnvironmentWrapperImpl
49 simple_task_env_impl(jvm, java_simple_task_env);
50
51 std::string population_name =
52 MoreJniUtil::JStringToString(env, population_name_jstring);
53 std::string session_name =
54 MoreJniUtil::JStringToString(env, session_name_jstring);
55 std::string task_name = MoreJniUtil::JStringToString(env, task_name_jstring);
56 // TODO: add real implementation of log manager.
57 fcp::client::engine::jni::LogManagerWrapperImpl log_manager_impl(
58 jvm, java_native_log_manager);
59 std::string checkpoint_input_filename =
60 MoreJniUtil::JStringToString(env, checkpoint_input_filename_jstring);
61 std::string checkpoint_output_filename =
62 MoreJniUtil::JStringToString(env, checkpoint_output_filename_jstring);
63 fcp::client::InterruptibleRunner::TimingConfig timing_config = {
64 .polling_period = absl::Milliseconds(1000),
65 };
66
67 absl::StatusOr<fcp::client::FLRunnerResult> fl_runner_result =
68 fcp::client::RunFederatedComputation(
69 &simple_task_env_impl, &log_manager_impl, &flags, client_only_plan,
70 checkpoint_input_filename, checkpoint_output_filename, session_name,
71 population_name, task_name, timing_config);
72 FCP_CHECK(fl_runner_result.ok());
73 // Serialize + return the FLRunnerResult.
74 jbyteArray fl_runner_result_serialized =
75 fcp::jni::SerializeProtoToJByteArray(env, fl_runner_result.value());
76 return fl_runner_result_serialized;
77 }
78