OPenPCDet windows process and its problems

First of all, it is extremely not recommended to run OPenPCDet on Windows . The process is very complicated and the adaptation is not very good. It is not recommended to download and train on Windows. I do this mainly to facilitate the sorting out of the experimental results on the laptop and processing some Simple reasoning evaluation and other tasks. Read on if necessary:


Here is the text:

The general installation process will not be described in detail, please refer to the official or other blog posts.

  1. First of all, in the dependency file (requirement.txt), the SharedArray pip installation will fail because its default version is not the windows version: https://github.com/imaginary-friend94/Shared-Array-for-Windows

It is available on github, but it needs to be changed after downloading, so it is placed like this:

Leave SharedArray

You can also directly copy my modified version in one step:

Build according to the above arrangement: setup.py

from setuptools import setup, find_packages, Extension
import numpy as np
import sys

if sys.platform == "linux" or sys.platform == "linux2":
    libraries_ext = ["rt"]
elif sys.platform == "win32":
    libraries_ext = []


ext_modules = [
    Extension('SharedArray',
    #extra_compile_args=["-std=c++11"],  #这里更改了c++ 11,我这里使用这种方法会报错,故而取消这个
    sources = ['shared_memory_python.cpp'],
    libraries = libraries_ext,
    language="c++")    #增加了编译语言
]

setup(
        name = 'SharedArray',
        version = '1.2',
        include_dirs = [np.get_include()], #Add Include path of numpy
        ext_modules = ext_modules
      )

build shared_memory_python.cpp

#if defined(_WIN64) || defined(_WIN32) || defined(__CYGWIN__)
#define WIN
#include <Windows.h>
#include <winbase.h>
#include <sddl.h>
#pragma comment(lib, "advapi32.lib")
#elif defined(__linux__)
#define LINUX
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>

struct sem_wrapper
{
    sem_t * sem;
    bool is_locked;
};

#endif

#include "Python.h"
#include "numpy/arrayobject.h"
#include <iostream>
#include <cstdio>
#include <cstring>

#define WIN_SMEM "WINDOWS SHARED MEMORY"
#define ARRAY_STRUCT_SIZE sizeof(PyArrayObject)
#define ARRAY_FULL_SIZE(arr) (size_data_array(arr) + sizeof(int) + arr->nd * sizeof(npy_intp) * 3 + sizeof(int))


#if defined(WIN)

BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
{
     TCHAR * szSD = TEXT("D:")       // Discretionary ACL
        TEXT("(D;OICI;GA;;;BG)")     // Deny access to 
                                     // built-in guests
        TEXT("(D;OICI;GA;;;AN)")     // Deny access to 
                                     // anonymous logon
        TEXT("(A;OICI;GRGWGX;;;AU)") // Allow 
                                     // read/write/execute 
                                     // to authenticated 
                                     // users
        TEXT("(A;OICI;GA;;;BA)");    // Allow full control 
                                     // to administrators

    if (NULL == pSA)
        return FALSE;

     return ConvertStringSecurityDescriptorToSecurityDescriptor(
                szSD,
                SDDL_REVISION_1,
                &(pSA->lpSecurityDescriptor),
                NULL);
}
#endif

/*
 * copy_from_pointer_array
 * the method returns the number of bytes copied
 */
template <typename ObjectType>
std::size_t copy_from_pointer_array(ObjectType * buffer_dist, ObjectType * buffer_src, size_t len) {
    for (int i = 0; i < len; i++) {
        *buffer_dist = *buffer_src;
        buffer_dist++;
        buffer_src++;
    }
    return len * sizeof(ObjectType);
}

std::size_t size_data_array(PyArrayObject *arr) {
    if (arr->nd == 0)
        return 0;

    std::size_t size = 1;
    for (int i = 0; i < arr->nd; ++i) {
        size *= (int) PyArray_DIM(arr, i);
    }
    size *= PyArray_ITEMSIZE(arr);
    return size;
}

void copy_from_numpy_array_to_buffer(PyArrayObject * array, char * buffer) {
    char * current_pointer = buffer;
    *((int *) current_pointer) = array->nd;
    current_pointer += sizeof(int);
    // dimensions copy
    current_pointer += copy_from_pointer_array(
        (npy_intp * ) current_pointer, 
        (npy_intp * ) array->dimensions, 
        array->nd
    );
    // strides copy
    current_pointer += copy_from_pointer_array(
        (npy_intp * ) current_pointer, 
        (npy_intp * ) array->strides, 
        array->nd
    );
    *((int *) current_pointer) = array->descr->type_num; 
    current_pointer += sizeof(int);

    size_t size_data = size_data_array(array);
    /* Copy data from heap to mmap memory */
    std::memcpy((char *) (current_pointer), (char *) array->data, size_data);
}

PyArrayObject * copy_from_buffer_to_numpy_array(char * buffer) {
    char * current_pointer = buffer;
    int nd = *((int *) current_pointer);
    current_pointer += sizeof(int);
    npy_intp * dims = new npy_intp[nd];

    current_pointer += copy_from_pointer_array(
        (npy_intp * ) dims, 
        (npy_intp * ) current_pointer, 
        nd
    );
    npy_intp * strides = new npy_intp[nd];
    current_pointer += copy_from_pointer_array(
        (npy_intp * ) strides, 
        (npy_intp * ) current_pointer, 
        nd
    );
    int type_num = *((int *) current_pointer);
    current_pointer += sizeof(int);

    PyArrayObject * array = (PyArrayObject *) PyArray_SimpleNewFromData(nd, 
        dims, 
        type_num, 
        (void *) current_pointer
    );
    return array;
}


/*
 * Create a buffer in shared memory
 */
char * create_shared_memory(char * string_shm, int max_buffer_size) {
    bool error_open_file_flag = false;
#if defined(WIN)
    SECURITY_ATTRIBUTES sa;
    if (!CreateMyDACL(&sa))
    {
         // Error encountered; generate message and exit.
        PyErr_SetString(PyExc_RuntimeError, "create_mutex: failed CreateMyDACL");
        return nullptr;
    }
    HANDLE hMapFile;
    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,
        &sa,
        PAGE_READWRITE,
        0,
        max_buffer_size,
        string_shm);
    if (hMapFile == NULL) error_open_file_flag = true;
#elif defined(LINUX)
    int hMapFile = shm_open(string_shm, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (hMapFile < 0){ 
        error_open_file_flag = true; 
    } else {
        if (ftruncate(hMapFile, max_buffer_size) == -1) error_open_file_flag = true;
    }
#endif

    if (error_open_file_flag) {
        PyErr_SetString(PyExc_RuntimeError, "create file is failed");
        return nullptr;
    }

#if defined(WIN)
    char * pBuf = (char *) MapViewOfFile(hMapFile,
                        FILE_MAP_ALL_ACCESS,
                        0,
                        0,
                        max_buffer_size);
#elif defined(LINUX)
    char * pBuf = (char *) mmap(NULL, max_buffer_size, PROT_WRITE | PROT_READ, MAP_SHARED, hMapFile, 0);
#endif

    if (pBuf == nullptr) {
        PyErr_SetString(PyExc_RuntimeError, "memory not allocated");
        return nullptr;
    }

    return pBuf;
}
/*
 * Del a buffer in shared memory
 */
bool delete_shared_memory(char * string_shm) {
#if defined(WIN)
    return true;
#elif defined(LINUX)
    if (shm_unlink(string_shm) == 0) return true;
#endif
}

/*
 * Attach a buffer in shared memory
 */
char * attach_shared_memory(char * string_shm) {
    bool error_open_file_flag = false;
#if defined(WIN)
    HANDLE hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,
        FALSE,
        string_shm); 
    if (hMapFile == NULL) error_open_file_flag = true;
#elif defined(LINUX)
    int hMapFile = shm_open(string_shm, O_RDWR, 0);
    if (hMapFile == -1) error_open_file_flag = true;
#endif

    if (error_open_file_flag) {
        PyErr_SetString(PyExc_RuntimeError, "memory not attached");
        return nullptr;
    }

#if defined(WIN)
    char * pBuf = (char *) MapViewOfFile(hMapFile,
                        FILE_MAP_ALL_ACCESS,
                        0,
                        0,
                        sizeof(size_t));
#elif defined(LINUX)
    char * pBuf = (char *) mmap(0, sizeof(size_t), PROT_WRITE | PROT_READ, MAP_SHARED, hMapFile, 0);
#endif

    size_t full_array_size = *((size_t *) pBuf);

#if defined(WIN)
    UnmapViewOfFile((LPCVOID) pBuf);
    pBuf = (char *) MapViewOfFile(hMapFile,
                        FILE_MAP_ALL_ACCESS,
                        0,
                        0,
                        full_array_size);
#elif defined(LINUX)
    munmap(pBuf, sizeof(size_t));
    pBuf = (char *) mmap(0, full_array_size, PROT_WRITE | PROT_READ, MAP_SHARED, hMapFile, 0);
#endif

    pBuf += sizeof(size_t);

    if (pBuf == nullptr) {
        PyErr_SetString(PyExc_RuntimeError, "memory not attached");
        return nullptr;
    }

    return pBuf;
}

static PyObject *
check_mem_sh(PyObject *self, PyObject *args) 
{
    char * string_shm;
    if (!PyArg_ParseTuple(args, "s", &string_shm)) {
        PyErr_SetString(PyExc_RuntimeError, "set_mem_sh: parse except");
    }
    bool error_open_file_flag = false;

#if defined(WIN)
    HANDLE hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,
        FALSE,
        string_shm);
    if (hMapFile == NULL) error_open_fi

Guess you like

Origin blog.csdn.net/m0_46611008/article/details/129006559