Skip to content

Commit b5a1c14

Browse files
author
chengduo
authored
Update CPU_NUM config (#18059)
* update CPU_NUM config test=develop
1 parent f8ecc3d commit b5a1c14

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

paddle/fluid/framework/parallel_executor.cc

+6
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,12 @@ ParallelExecutor::ParallelExecutor(const std::vector<platform::Place> &places,
328328
"the number of places must be greater than 1.");
329329
}
330330

331+
LOG(WARNING) << string::Sprintf(
332+
"The number of %s, which is used in ParallelExecutor, is %lu. And "
333+
"the Program will be copied %lu copies",
334+
(member_->use_cuda_ ? "CUDAPlace" : "CPUPlace"), places.size(),
335+
places.size());
336+
331337
// Step 1. Bcast the bcast_vars to devs.
332338
// Create local scopes
333339
if (local_scopes.empty()) {

python/paddle/dataset/flowers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ def reader():
138138
break
139139

140140
if use_xmap:
141-
cpu_num = int(os.environ.get('CPU_NUM', cpu_count()))
142-
return xmap_readers(mapper, reader, cpu_num, buffered_size)
141+
return xmap_readers(mapper, reader, min(4, cpu_count()), buffered_size)
143142
else:
144143
return map_readers(mapper, reader)
145144

python/paddle/fluid/contrib/slim/tests/test_graph_wrapper.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import numpy as np
2020
from paddle.fluid.contrib.slim.graph import GraphWrapper
2121
from paddle.fluid import core
22+
import os
23+
os.environ['CPU_NUM'] = str(4)
2224

2325

2426
def residual_block(num):

python/paddle/fluid/data_feeder.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import multiprocessing
2323

2424
from .framework import Variable, default_main_program, _current_expected_place
25-
25+
from .framework import _cpu_num, _cuda_ids
2626
__all__ = ['DataFeeder']
2727

2828

@@ -359,11 +359,9 @@ def _get_number_of_places_(self, num_places):
359359
if num_places is not None:
360360
return int(num_places)
361361
elif isinstance(self.place, core.CUDAPlace):
362-
return core.get_cuda_device_count()
362+
return len(_cuda_ids())
363363
else:
364-
cpu_num = int(
365-
os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
366-
return cpu_num
364+
return _cpu_num()
367365

368366
def decorate_reader(self,
369367
reader,

python/paddle/fluid/framework.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,24 @@ def _current_expected_place():
8282

8383

8484
def _cpu_num():
85-
return int(os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
85+
cpu_num = os.environ.get('CPU_NUM', None)
86+
if cpu_num is None:
87+
sys.stderr.write(
88+
'The CPU_NUM is not specified, you should set CPU_NUM in '
89+
'the environment variable list, i.e export CPU_NUM=1. CPU_NUM '
90+
'indicates that how many CPUPlace are used in the current task.\n'
91+
'!!! The default number of CPUPlaces is 1.')
92+
os.environ['CPU_NUM'] = str(1)
93+
return int(cpu_num)
94+
95+
96+
def _cuda_ids():
97+
gpus_env = os.getenv("FLAGS_selected_gpus")
98+
if gpus_env:
99+
device_ids = [int(s) for s in gpus_env.split(",")]
100+
else:
101+
device_ids = six.moves.range(core.get_cuda_device_count())
102+
return device_ids
86103

87104

88105
def cuda_places(device_ids=None):
@@ -116,11 +133,7 @@ def cuda_places(device_ids=None):
116133
assert core.is_compiled_with_cuda(), \
117134
"Not compiled with CUDA"
118135
if device_ids is None:
119-
gpus_env = os.getenv("FLAGS_selected_gpus")
120-
if gpus_env:
121-
device_ids = [int(s) for s in gpus_env.split(",")]
122-
else:
123-
device_ids = six.moves.range(core.get_cuda_device_count())
136+
device_ids = _cuda_ids()
124137
elif not isinstance(device_ids, (list, tuple)):
125138
device_ids = [device_ids]
126139
return [core.CUDAPlace(dev_id) for dev_id in device_ids]

python/paddle/fluid/tests/unittests/test_parallel_executor_dry_run.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import unittest
1818
import logging
1919
import six
20+
import os
21+
os.environ['CPU_NUM'] = str(4)
2022

2123

2224
class TestBase(unittest.TestCase):

python/paddle/fluid/tests/unittests/test_py_reader_using_executor.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import threading
2323
import multiprocessing
2424
import os
25+
os.environ['CPU_NUM'] = str(4)
2526

2627

2728
def as_tensor(np_array_or_tensor, place=None):

0 commit comments

Comments
 (0)