|
| 1 | +# Copyright 1999-2021 Alibaba Group Holding Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import mars |
| 16 | +from .... import tensor as mt |
| 17 | +from .... import dataframe as md |
| 18 | +from ....tests.core import require_ray |
| 19 | +from ....utils import lazy_import |
| 20 | +from ..ray import ( |
| 21 | + new_cluster_in_ray, |
| 22 | + new_ray_session, |
| 23 | +) |
| 24 | + |
| 25 | +ray = lazy_import("ray") |
| 26 | + |
| 27 | + |
| 28 | +@require_ray |
| 29 | +def test_new_cluster_in_ray(stop_ray): |
| 30 | + cluster = new_cluster_in_ray(worker_num=2) |
| 31 | + mt.random.RandomState(0).rand(100, 5).sum().execute() |
| 32 | + cluster.session.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 33 | + mars.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 34 | + session = new_ray_session(address=cluster.address, session_id="abcd", default=True) |
| 35 | + session.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 36 | + mars.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 37 | + cluster.stop() |
| 38 | + |
| 39 | + |
| 40 | +@require_ray |
| 41 | +def test_new_ray_session(stop_ray): |
| 42 | + new_ray_session_test() |
| 43 | + |
| 44 | + |
| 45 | +def new_ray_session_test(): |
| 46 | + session = new_ray_session(session_id="abc", worker_num=2) |
| 47 | + mt.random.RandomState(0).rand(100, 5).sum().execute() |
| 48 | + session.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 49 | + mars.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 50 | + session = new_ray_session(session_id="abcd", worker_num=2, default=True) |
| 51 | + session.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 52 | + mars.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
| 53 | + df = md.DataFrame(mt.random.rand(100, 4), columns=list("abcd")) |
| 54 | + # Convert mars dataframe to ray dataset |
| 55 | + ds = md.to_ray_dataset(df) |
| 56 | + print(ds.schema(), ds.count()) |
| 57 | + ds.filter(lambda row: row["a"] > 0.5).show(5) |
| 58 | + # Convert ray dataset to mars dataframe |
| 59 | + df2 = md.read_ray_dataset(ds) |
| 60 | + print(df2.head(5).execute()) |
| 61 | + # Test ray cluster exists after session got gc. |
| 62 | + del session |
| 63 | + import gc |
| 64 | + |
| 65 | + gc.collect() |
| 66 | + mars.execute(mt.random.RandomState(0).rand(100, 5).sum()) |
0 commit comments