Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Migration #219

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
startup --windows_enable_symlinks
build --enable_runfiles
build --action_env=BAZEL_LINKLIBS=-l%:libstdc++.a:-lm
build --action_env=BAZEL_LINKOPTS=-static-libgcc
build --action_env=CUDA_DIR=/usr/local/cuda
build --incompatible_strict_action_env --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --client_env=BAZEL_CXXOPTS=-std=c++17
# build --action_env=CUDA_DIR=/usr/local/cuda
build --incompatible_strict_action_env --cxxopt=/std:c++17 --host_cxxopt=/std:c++17 --client_env=BAZEL_CXXOPTS=/std:c++17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure the best practice here, @mavenlin can you take a look?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember tensorflow use the same mechanism, but they have some fancy config script to detect cuda and generate the bazelrc.

build:debug --cxxopt=-DENVPOOL_TEST --compilation_mode=dbg -s
build:test --cxxopt=-DENVPOOL_TEST --copt=-g0 --copt=-O3 --copt=-DNDEBUG --copt=-msse --copt=-msse2 --copt=-mmmx
build:release --copt=-g0 --copt=-O3 --copt=-DNDEBUG --copt=-msse --copt=-msse2 --copt=-mmmx
Expand Down
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ py_binary(
requirement("wheel"),
],
)

30 changes: 30 additions & 0 deletions WINDOWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Windows Migration

Note that this merge request only intends to work on Windows. I haven't write code that switch support between linux and Windows. I'll update that feature after the whole thing can be build on Windows.

## Setup Environment On Windows

1. Install [Windows Powershell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.2). Do step 2 and 3 in Powershell.
2. Install [choco](https://chocolatey.org/install#individual)
3. Install bazelisk/bazel ``` choco install bazelisk```
4. Install [MinGW-w64](https://www.mingw-w64.org/)
5. Install [git bash](https://gitforwindows.org/) and make sure it uses MINGW64 emulation. Do all the following steps in bash.
6. Install [VC++ 2017](https://www.google.com/url?q=https://aka.ms/vs/15/release/vs_buildtools.exe&sa=D&source=docs&ust=1667504254128145&usg=AOvVaw3jOTmoHfuzAHgaScSgsmlE) on Windows
7. Install python 3.10 (instead of 3.11 because pygame installation fails on Windows)
8. Go where python.exe is installed, make a copy of python.exe and rename it python3.exe (it would bypass the bug "python interpreter cannot be found with requirement")
9. Install [swig.exe](https://sourceforge.net/projects/swig/files/swigwin/swigwin-4.1.0/swigwin-4.1.0.zip/download?use_mirror=gigenet) and merge it into python folder and do [some hacks](https://stackoverflow.com/questions/44504899/installing-pocketsphinx-python-module-command-swig-exe-failed)
10. Install [Clang](https://bazel.build/configure/windows#using) and follow this [link](https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.2). Don't forget to set BAZEL_VC and BAZEL_LLVM in environmental variable manually.
11. Now you can run some tests
```bazel build --test_output=all //envpool/dummy:dummy_envpool_test --config=debug --compiler=clang-cl```



## Parts that built successfully
- [x] Dummy
- [x] Utils
- [x] Box2d
- [x] Classic Control
- [x] Mujoco:mujoco_gym-env
- [ ] Mujoco:mujoco_dmc_env: no matching function for call to 'mj_loadXML', use of undeclared identifier 'M_PI'...
- [ ] Atari:atari_env #include <dlfcn.h> not found
- [ ] Toy_text fatal error: 'cuda_runtime_api.h' file not found
2 changes: 1 addition & 1 deletion docs/content/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ It also requires **Python version >= 3.7**:
sudo apt install -y python3-dev python3-pip
sudo ln -sf /usr/bin/python3 /usr/bin/python

Install CUDA to enable XLA: see https://developer.nvidia.com/cuda-downloads
Install CUDA to enable XLA: see https://developer.nvidia.com/-downloads

Install other dependencies: see
`Dockerfile <https://github.com/sail-sg/envpool/tree/main/docker>`_.
Expand Down
4 changes: 3 additions & 1 deletion envpool/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ cc_library(
],
)

# if windows, makes up a dummy xla without cuda dependency
cc_library(
name = "xla_template",
hdrs = ["xla_template.h"],
Expand All @@ -183,6 +184,7 @@ cc_library(
],
)


cc_library(
name = "xla",
hdrs = ["xla.h"],
Expand All @@ -198,6 +200,6 @@ pybind_library(
hdrs = ["py_envpool.h"],
deps = [
":envpool",
":xla",
":xla"
],
)
18 changes: 10 additions & 8 deletions envpool/core/async_envpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ class AsyncEnvPool : public EnvPool<typename Env::Spec> {
if (spec.config["thread_affinity_offset"_] >= 0) {
std::size_t thread_affinity_offset =
spec.config["thread_affinity_offset"_];
for (std::size_t tid = 0; tid < num_threads_; ++tid) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
std::size_t cid = (thread_affinity_offset + tid) % processor_count;
CPU_SET(cid, &cpuset);
pthread_setaffinity_np(workers_[tid].native_handle(), sizeof(cpu_set_t),
&cpuset);
}

// The following does not work under Windows. Need to find alternatives.
// for (std::size_t tid = 0; tid < num_threads_; ++tid) {
// cpu_set_t cpuset;
// CPU_ZERO(&cpuset);
// std::size_t cid = (thread_affinity_offset + tid) % processor_count;
// CPU_SET(cid, &cpuset);
// pthread_setaffinity_np(workers_[tid].native_handle(), sizeof(cpu_set_t),
// &cpuset);
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use #ifdef

}
}

Expand Down
5 changes: 4 additions & 1 deletion envpool/core/state_buffer_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ class StateBufferQueue {
}
std::size_t processor_count = std::thread::hardware_concurrency();
// hardcode here :(
std::size_t create_buffer_thread_num = std::max(1UL, processor_count / 64);
std::size_t create_buffer_thread_num = 1;
if (processor_count / 64 > create_buffer_thread_num){
create_buffer_thread_num = processor_count / 64;
}
for (std::size_t i = 0; i < create_buffer_thread_num; ++i) {
create_buffer_thread_.emplace_back(std::thread([&]() {
while (true) {
Expand Down
8 changes: 5 additions & 3 deletions envpool/core/xla.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#ifndef ENVPOOL_CORE_XLA_H_
#define ENVPOOL_CORE_XLA_H_

#include <cuda_runtime_api.h>
#include "envpool/core/array.h"
#include "envpool/core/xla_template.h"
#include "cuda_runtime_api.h"

#include <cstdint>
#include <memory>
Expand All @@ -26,8 +28,8 @@
#include <tuple>
#include <vector>

#include "envpool/core/array.h"
#include "envpool/core/xla_template.h"



template <typename D>
constexpr bool is_container_v = false; // NOLINT
Expand Down
3 changes: 2 additions & 1 deletion envpool/core/xla_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifndef ENVPOOL_CORE_XLA_TEMPLATE_H_
#define ENVPOOL_CORE_XLA_TEMPLATE_H_

#include <cuda_runtime_api.h>
#include "cuda_runtime_api.h"
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -109,6 +109,7 @@ struct CustomCall {
return std::make_tuple(
py::capsule(reinterpret_cast<void*>(Cpu), "xla._CUSTOM_CALL_TARGET"),
py::capsule(reinterpret_cast<void*>(Gpu), "xla._CUSTOM_CALL_TARGET"));

}

static auto Xla(Class* obj) {
Expand Down
1 change: 1 addition & 0 deletions envpool/mujoco/dmc/mujoco_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <random>
#include <string>
#include <array>

#include "envpool/mujoco/dmc/utils.h"

Expand Down
1 change: 1 addition & 0 deletions envpool/toy_text/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pybind_extension(
deps = [
":toy_text_env",
"//envpool/core:py_envpool",
"//envpool/core:xla",
],
)

Expand Down
17 changes: 13 additions & 4 deletions envpool/workspace0.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//third_party/cuda:cuda.bzl", "cuda_configure")


def workspace():
"""Load requested packages."""
maybe(
Expand Down Expand Up @@ -318,13 +319,21 @@ def workspace():

maybe(
http_archive,
name = "mujoco",
name = "mujoco_linux",
sha256 = "d1cb3a720546240d894cd315b7fd358a2b96013a1f59b6d718036eca6f6edac2",
strip_prefix = "mujoco-2.2.1",
urls = [
"https://github.com/deepmind/mujoco/releases/download/2.2.1/mujoco-2.2.1-linux-x86_64.tar.gz",
"https://ml.cs.tsinghua.edu.cn/~jiayi/envpool/deepmind/mujoco/mujoco-2.2.1-linux-x86_64.tar.gz",
],
"https://github.com/deepmind/mujoco/releases/download/2.2.1/mujoco-2.2.1-linux-x86_64.tar.gz",
"https://ml.cs.tsinghua.edu.cn/~jiayi/envpool/deepmind/mujoco/mujoco-2.2.1-linux-x86_64.tar.gz",
],
build_file = "//third_party/mujoco:mujoco.BUILD",
)

maybe(
http_archive,
name = "mujoco",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name = "mujoco",
name = "mujoco_windows",

sha256 = "e0068d25fdb00ec3afa53e429b5eff438d222455603bafa080086abaf11fe16f",
urls = ["https://github.com/deepmind/mujoco/releases/download/2.2.1/mujoco-2.2.1-windows-x86_64.zip"],
build_file = "//third_party/mujoco:mujoco.BUILD",
)

Expand Down
8 changes: 5 additions & 3 deletions third_party/cuda/cuda.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
_CUDA_DIR = "CUDA_DIR"

def _impl(rctx):
cuda_dir = rctx.os.environ.get(_CUDA_DIR, default = "/usr/local/cuda")
cuda_dir = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7"
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
# rctx.os.environ.get(_CUDA_DIR, default = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.7")
rctx.symlink("{}/include".format(cuda_dir), "include")
rctx.symlink("{}/lib64".format(cuda_dir), "lib64")
rctx.symlink("{}/lib".format(cuda_dir), "lib")
rctx.file("WORKSPACE")
rctx.file("BUILD", content = """
package(default_visibility = ["//visibility:public"])

cc_library(
name = "cudart_static",
srcs = ["lib64/libcudart_static.a"],
srcs = ["lib/x64/cudart_static.lib"],
hdrs = glob([
"include/*.h",
"include/**/*.h",
Expand Down
4 changes: 0 additions & 4 deletions third_party/vizdoom/vizdoom.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ cc_library(
hdrs = glob(["gdtoa/*.h"]),
copts = [
"-Wall",
"-Wextra",
"-DINFNAN_CHECK",
"-DMULTIPLE_THREADS",
"-include $(execpath @glibc_version_header//:glibc_2_17)",
Expand All @@ -78,7 +77,6 @@ cc_library(
copts = [
"-DBZ_NO_STDIO",
"-Wall",
"-Wextra",
"-fomit-frame-pointer",
"-include $(execpath @glibc_version_header//:glibc_2_17)",
],
Expand Down Expand Up @@ -120,7 +118,6 @@ cc_library(
copts = [
"-D_7ZIP_ST",
"-Wall",
"-Wextra",
"-fomit-frame-pointer",
"-D_7ZIP_PPMD_SUPPPORT",
"-include $(execpath @glibc_version_header//:glibc_2_17)",
Expand Down Expand Up @@ -211,7 +208,6 @@ cc_library(
copts = [
"-DNEED_ITOA=1",
"-Wall",
"-Wno-pointer-sign",
"-Wno-uninitialized",
"-Wno-unused-but-set-variable",
"-include $(execpath @glibc_version_header//:glibc_2_17)",
Expand Down
1 change: 0 additions & 1 deletion third_party/zlib/zlib.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ cc_library(
],
hdrs = ["zlib.h"],
copts = [
"-Wno-shift-negative-value",
"-DZ_HAVE_UNISTD_H",
],
includes = ["."],
Expand Down