Skip to content

Commit d361624

Browse files
authored
platform module (#12932)
* platform module * Update profiler.h
1 parent d292ad8 commit d361624

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

paddle/fluid/platform/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
if (NOT WIN32)
12
proto_library(profiler_proto SRCS profiler.proto DEPS framework_proto)
23
py_proto_compile(profiler_py_proto SRCS profiler.proto)
34

@@ -10,6 +11,7 @@ add_custom_command(TARGET profiler_py_proto POST_BUILD
1011
COMMAND cp *.py ${PADDLE_BINARY_DIR}/python/paddle/fluid/proto/profiler
1112
COMMENT "Copy generated python proto into directory paddle/fluid/proto/profiler."
1213
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
14+
endif(NOT WIN32)
1315

1416
if(WITH_GPU)
1517
nv_library(enforce SRCS enforce.cc)
@@ -58,9 +60,12 @@ cc_test(init_test SRCS init_test.cc DEPS device_context)
5860
nv_test(cudnn_helper_test SRCS cudnn_helper_test.cc DEPS dynload_cuda)
5961
nv_test(transform_test SRCS transform_test.cu DEPS memory place device_context)
6062

63+
64+
if (NOT WIN32)
6165
cc_library(device_tracer SRCS device_tracer.cc DEPS boost profiler_proto framework_proto ${GPU_CTX_DEPS})
6266
cc_library(profiler SRCS profiler.cc DEPS device_context device_tracer)
6367
cc_test(profiler_test SRCS profiler_test.cc DEPS profiler)
68+
endif(NOT WIN32)
6469

6570
nv_test(float16_gpu_test SRCS float16_test.cu DEPS lod_tensor)
6671
cc_test(float16_test SRCS float16_test.cc DEPS lod_tensor)

paddle/fluid/platform/cpu_info.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,34 @@ limitations under the License. */
2222
#ifdef __APPLE__
2323
#include <sys/sysctl.h>
2424
#include <sys/types.h>
25+
26+
#elif defined(_WIN32)
27+
#define NOMINMAX // msvc max/min macro conflict with std::min/max
28+
#include <windows.h>
2529
#else
2630
#include <unistd.h>
27-
#endif
31+
#endif // _WIN32
2832

2933
#include <algorithm>
3034
#include "gflags/gflags.h"
3135

3236
DEFINE_double(fraction_of_cpu_memory_to_use, 1,
3337
"Default use 100% of CPU memory for PaddlePaddle,"
3438
"reserve the rest for page tables, etc");
35-
39+
#if !defined(_WIN32)
3640
DEFINE_uint64(initial_cpu_memory_in_mb,
3741
#ifdef PADDLE_WITH_MKLDNN
3842
/* Aligned with mozga-intel, MKLDNN need at least 5000 MB
3943
* to obtain the best performance*/
40-
5000,
44+
5000ul,
4145
#else
42-
500,
46+
500ul,
4347
#endif
4448
"Initial CPU memory for PaddlePaddle, in MD unit.");
49+
#else
50+
DEFINE_uint64(initial_cpu_memory_in_mb, 500ul,
51+
"Initial CPU memory for PaddlePaddle, in MD unit.");
52+
#endif // !defined(_WIN32)
4553

4654
DEFINE_double(
4755
fraction_of_cuda_pinned_memory_to_use, 0.5,
@@ -60,6 +68,11 @@ inline size_t CpuTotalPhysicalMemory() {
6068
size_t len = sizeof(size);
6169
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) return (size_t)size;
6270
return 0L;
71+
#elif defined(_WIN32)
72+
MEMORYSTATUSEX sMeminfo;
73+
sMeminfo.dwLength = sizeof(sMeminfo);
74+
GlobalMemoryStatusEx(&sMeminfo);
75+
return sMeminfo.ullTotalPhys;
6376
#else
6477
int64_t pages = sysconf(_SC_PHYS_PAGES);
6578
int64_t page_size = sysconf(_SC_PAGE_SIZE);

paddle/fluid/platform/device_tracer.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414
#pragma once
1515

16+
#if !defined(_WIN32)
1617
#include <sys/time.h>
18+
#else
19+
#include <windows.h>
20+
#endif // !_WIN32
21+
1722
#include <time.h>
1823
#include <chrono> // NOLINT
1924
#include <string>
@@ -27,12 +32,15 @@ namespace platform {
2732
///////////////////////
2833
// WARN: Under Development. Don't depend on it yet.
2934
//////////////////////
30-
35+
#if !defined(_WIN32)
3136
inline uint64_t PosixInNsec() {
3237
struct timeval tv;
3338
gettimeofday(&tv, nullptr);
3439
return 1000 * (static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec);
3540
}
41+
#else
42+
inline uint64_t PosixInNsec() { return static_cast<uint64_t>(0); }
43+
#endif // !_WIN32
3644

3745
// DeviceTracer performs the following tasks:
3846
// 1. Register cuda callbacks for various events: kernel, memcpy, etc.

paddle/fluid/platform/dynload/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ if (CUPTI_FOUND)
1616
list(APPEND CUDA_SRCS cupti.cc)
1717
endif(CUPTI_FOUND)
1818
nv_library(dynload_cuda SRCS ${CUDA_SRCS} DEPS dynamic_loader)
19+
if (NOT WIN32)
1920
cc_library(dynload_warpctc SRCS warpctc.cc DEPS dynamic_loader warpctc)
21+
endif(NOT WIN32)
2022
if (WITH_MKLML)
2123
cc_library(dynload_mklml SRCS mklml.cc DEPS dynamic_loader mklml)
2224
endif()

paddle/fluid/platform/dynload/dynamic_loader.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ See the License for the specific language governing permissions and
1313
limitations under the License. */
1414
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
1515

16-
#include <dlfcn.h>
17-
1816
#include <memory>
1917
#include <mutex> // NOLINT
2018
#include <string>
@@ -23,6 +21,7 @@ limitations under the License. */
2321
#include "glog/logging.h"
2422
#include "paddle/fluid/platform/dynload/cupti_lib_path.h"
2523
#include "paddle/fluid/platform/enforce.h"
24+
#include "paddle/fluid/platform/port.h"
2625

2726
DEFINE_string(cudnn_dir, "",
2827
"Specify path for loading libcudnn.so. For instance, "

paddle/fluid/platform/enforce.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ limitations under the License. */
1818
#include <cxxabi.h> // for __cxa_demangle
1919
#endif // __GNUC__
2020

21+
#if defined(_WIN32)
22+
#define NOMINMAX // msvc max/min macro conflict with std::min/max
23+
#define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h
24+
#endif
25+
2126
#ifdef PADDLE_WITH_CUDA
2227
#include <cublas_v2.h>
2328
#include <cudnn.h>
@@ -117,7 +122,12 @@ struct EOFException : public std::exception {
117122
// always forces branch prediction of true.
118123
// This generates faster binary code. __builtin_expect is since C++11.
119124
// For more details, please check https://stackoverflow.com/a/43870188/724872.
125+
#if !defined(_WIN32)
120126
#define UNLIKELY(condition) __builtin_expect(static_cast<bool>(condition), 0)
127+
#else
128+
// there is no equivalent intrinsics in msvc.
129+
#define UNLIKELY(condition) (condition == 0)
130+
#endif
121131

122132
template <typename... Args>
123133
inline typename std::enable_if<sizeof...(Args) != 0, void>::type throw_on_error(
@@ -230,6 +240,7 @@ inline void throw_on_error(T e) {
230240
throw_on_error(e, "");
231241
}
232242

243+
#if !defined(_WIN32)
233244
#define PADDLE_THROW(...) \
234245
do { \
235246
throw ::paddle::platform::EnforceNotMet( \
@@ -248,15 +259,28 @@ inline void throw_on_error(T e) {
248259
__FILE__, __LINE__); \
249260
} \
250261
} while (false)
251-
#else
252-
#define PADDLE_ENFORCE(...) ::paddle::platform::throw_on_error(__VA_ARGS__);
253-
#endif
254262

255263
#define PADDLE_THROW_EOF() \
256264
do { \
257265
throw ::paddle::platform::EOFException("There is no next data.", __FILE__, \
258266
__LINE__); \
259267
} while (false)
268+
269+
#else
270+
#define PADDLE_ENFORCE(...) ::paddle::platform::throw_on_error(__VA_ARGS__)
271+
#endif // REPLACE_ENFORCE_GLOG
272+
273+
#else // !_WIN32
274+
// disable enforce, caused by the varardic macro exception error
275+
#define PADDLE_THROW(x) \
276+
do { \
277+
throw std::make_exception_ptr( \
278+
std::runtime_error("Windows disable the enforce.")); \
279+
} while (false)
280+
281+
#define PADDLE_ENFORCE(x, ...) x
282+
#endif // !_WIN32
283+
260284
/*
261285
* Some enforce helpers here, usage:
262286
* int a = 1;

paddle/fluid/platform/profiler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void PushEvent(const std::string& name, const DeviceContext* dev_ctx);
6969

7070
void PopEvent(const std::string& name, const DeviceContext* dev_ctx);
7171

72+
#if !defined(_WIN32)
7273
struct RecordEvent {
7374
RecordEvent(const std::string& name, const DeviceContext* dev_ctx);
7475

@@ -94,6 +95,15 @@ struct RecordBlock {
9495
std::string name_;
9596
uint64_t start_ns_;
9697
};
98+
#else
99+
// windows do not support profiler temporarily.
100+
struct RecordEvent {
101+
RecordEvent(const std::string& name, const DeviceContext* dev_ctx) {}
102+
};
103+
struct RecordBlock {
104+
explicit RecordBlock(int block_id) {}
105+
};
106+
#endif
97107

98108
// Return the event list of all threads. Assumed the returned value calls
99109
// event_lists, event_lists[i][j] represents the j-th Event of i-th thread.

0 commit comments

Comments
 (0)