-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7250ea9
commit ccde2d8
Showing
2 changed files
with
57 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
""" | ||
The utils functions for joblib spark backend. | ||
""" | ||
|
||
|
||
# pylint: disable=import-outside-toplevel | ||
def get_spark_session(): | ||
""" | ||
Get the spark session from the active session or create a new one. | ||
""" | ||
from pyspark.sql import SparkSession | ||
|
||
spark_session = SparkSession.getActiveSession() | ||
if spark_session is None: | ||
spark_session = SparkSession \ | ||
.builder \ | ||
.appName("JoblibSparkBackend") \ | ||
.getOrCreate() | ||
return spark_session | ||
|
||
|
||
def create_resource_profile(num_cpus_worker_node, num_gpus_worker_node): | ||
""" | ||
Create a resource profile for the task. | ||
:param num_cpus_worker_node: Number of cpus for the Spark worker node. | ||
:param num_gpus_worker_node: Number of gpus for the Spark worker node. | ||
:return: Spark ResourceProfile | ||
""" | ||
from pyspark.resource.profile import ResourceProfileBuilder | ||
from pyspark.resource.requests import TaskResourceRequests | ||
|
||
task_res_req = TaskResourceRequests().cpus(num_cpus_worker_node) | ||
if num_gpus_worker_node > 0: | ||
task_res_req = task_res_req.resource("gpu", num_gpus_worker_node) | ||
return ResourceProfileBuilder().require(task_res_req).build |