Skip to content

Add and use python for formatting. #6298

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .ci/azure-pipelines/formatting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- checkout: self
# find the commit hash on a quick non-forced update too
fetchDepth: 10
- script: ./.dev/format.sh $(which clang-format) .
- script: python ./.dev/format.py .
displayName: 'Run clang-format'
- script: git diff > formatting.patch
displayName: 'Compute diff'
Expand Down
79 changes: 79 additions & 0 deletions .dev/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

import os
import sys
import subprocess
import argparse

EXTENSIONS = (".c", ".h", ".cpp", ".hpp", ".cxx", ".hxx", ".cu")
WHITELIST_FILE = os.path.join(".dev", "whitelist.txt")

def load_whitelist():
if not os.path.isfile(WHITELIST_FILE):
print(f"Could not find whitelist file at {WHITELIST_FILE}")
sys.exit(167)
with open(WHITELIST_FILE, "r") as f:
return [line.strip() for line in f if line.strip()]

def is_in_whitelist(file_path, whitelist):
file_path = os.path.normpath(file_path)
for w in whitelist:
w_norm = os.path.normpath(w)
if os.path.commonpath([file_path, w_norm]) == w_norm or file_path.startswith(w_norm + os.sep):
return True
return False

def find_files(path):
if os.path.isfile(path):
if path.endswith(EXTENSIONS):
yield path
elif os.path.isdir(path):
for root, _, files in os.walk(path):
for file in files:
if file.endswith(EXTENSIONS):
yield os.path.join(root, file)

def main():
parser = argparse.ArgumentParser(
description="Format C/C++/CUDA files using clang-format via pipx."
)
parser.add_argument(
"files",
nargs="+",
help="List of files to check/format, or a single '.' to check all whitelisted files."
)
args = parser.parse_args()

whitelist = load_whitelist()
manual_mode = len(args.files) == 1 and args.files[0] == "."

if manual_mode:
all_files = []
for rel_path in whitelist:
abs_path = os.path.join(os.getcwd(), rel_path)
all_files.extend(find_files(abs_path))
else:
all_files = [
f for f in args.files
if f.endswith(EXTENSIONS) and os.path.isfile(f) and is_in_whitelist(f, whitelist)
]

all_files = list(set(all_files)) # Remove duplicates

if not all_files:
print("No files found to format.")
sys.exit(0)

check_cmd = ["pipx", "run", "clang-format==14.0.3", "--dry-run", "--Werror", "--style=file"] + all_files
result = subprocess.run(check_cmd, capture_output=True, text=True)
if result.returncode != 0:
format_cmd = ["pipx", "run", "clang-format==14.0.3", "-i", "--style=file"] + all_files
subprocess.run(format_cmd)
if not manual_mode:
sys.exit(1) # Only fail for pre-commit
# In manual mode, just continue and exit 0

sys.exit(0)

if __name__ == "__main__":
main()
20 changes: 11 additions & 9 deletions .dev/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# $ sh /pcl/format.sh `which clang-format` /pcl

format() {
# don't use a directory with whitespace
local whitelist="apps/3d_rec_framework apps/in_hand_scanner apps/include apps/modeler apps/src benchmarks 2d geometry ml octree simulation stereo tracking registration gpu/containers gpu/segmentation"

local PCL_DIR="${2}"
local formatter="${1}"

Expand All @@ -18,17 +15,22 @@ format() {
exit 166
fi

# check for self
if [ ! -f "${PCL_DIR}/.dev/format.sh" ]; then
echo "Please ensure that PCL_SOURCE_DIR is passed as the second argument"
exit 166
fi

for dir in ${whitelist}; do
local whitelist_file="${PCL_DIR}/.dev/whitelist.txt"
if [ ! -f "${whitelist_file}" ]; then
echo "Could not find whitelist file at ${whitelist_file}"
exit 167
fi

while IFS= read -r dir || [ -n "$dir" ]; do
[ -z "$dir" ] && continue
path=${PCL_DIR}/${dir}
find ${path} -type f -iname *.[ch] -o -iname *.[ch]pp -o -iname *.[ch]xx \
-iname *.cu | xargs -n1 ${formatter} -i -style=file
done
find ${path} -type f \( -iname "*.[ch]" -o -iname "*.[ch]pp" -o -iname "*.[ch]xx" -o -iname "*.cu" \) | xargs -n1 ${formatter} -i -style=file
done < "${whitelist_file}"
}

format $@
format "$@"
16 changes: 16 additions & 0 deletions .dev/whitelist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apps/3d_rec_framework
apps/in_hand_scanner
apps/include
apps/modeler
apps/src
benchmarks
2d
geometry
ml
octree
simulation
stereo
tracking
registration
gpu/containers
gpu/segmentation
152 changes: 74 additions & 78 deletions gpu/containers/src/repacks.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,97 +3,94 @@

#include <algorithm>

namespace pcl
namespace pcl {
namespace device {
struct Info {
enum { SIZE = 4 };
int data[SIZE];
};

template <int n>
struct Point {
int data[n];
};

template <int in, int out, typename Info>
__global__ void
deviceCopyFields4B(const Info info, const int size, const void* input, void* output)
{
namespace device
{
struct Info
{
enum { SIZE = 4 };
int data[SIZE];
};

template<int n>
struct Point
{
int data[n];
};

template<int in, int out, typename Info>
__global__ void deviceCopyFields4B(const Info info, const int size, const void* input, void* output)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;

if (idx < size)
{
Point<in> point_in = reinterpret_cast<const Point<in>* >( input)[idx];
Point<out> point_out = reinterpret_cast< Point<out>* >(output)[idx];
int idx = blockIdx.x * blockDim.x + threadIdx.x;

for(int i = 0; i < Info::SIZE; ++i)
{
int byte;
int code = info.data[i];
if (idx < size) {
Point<in> point_in = reinterpret_cast<const Point<in>*>(input)[idx];
Point<out> point_out = reinterpret_cast<Point<out>*>(output)[idx];

byte = ((code >> 0) & 0xFF);
for (int i = 0; i < Info::SIZE; ++i) {
int byte;
int code = info.data[i];

if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];
byte = ((code >> 0) & 0xFF);

byte = ((code >> 8) & 0xFF);
if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];

if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];
byte = ((code >> 8) & 0xFF);

byte = ((code >> 16) & 0xFF);
if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];

if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];
byte = ((code >> 16) & 0xFF);

byte = ((code >> 24) & 0xFF);
if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];

if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];
}
byte = ((code >> 24) & 0xFF);

reinterpret_cast< Point<out>* >(output)[idx] = point_out;
}
};
if (byte == 0xFF)
break;
else
point_out.data[byte >> 4] = point_in.data[byte & 0xF];
}

template<int in_size, int out_size>
void cf(int info[4], int size, const void* input, void* output)
{
Info i;
std::copy(info, info + 4, i.data);
reinterpret_cast<Point<out>*>(output)[idx] = point_out;
}
};

dim3 block(256);
dim3 grid(divUp(size, block.x));
template <int in_size, int out_size>
void
cf(int info[4], int size, const void* input, void* output)
{
Info i;
std::copy(info, info + 4, i.data);

deviceCopyFields4B<in_size, out_size><<<grid, block>>>(i, size, input, output);
cudaSafeCall ( cudaGetLastError () );
cudaSafeCall (cudaDeviceSynchronize ());
}
dim3 block(256);
dim3 grid(divUp(size, block.x));

using copy_fields_t = void (*)(int info[4], int size, const void* input, void* output);
}
deviceCopyFields4B<in_size, out_size><<<grid, block>>>(i, size, input, output);
cudaSafeCall(cudaGetLastError());
cudaSafeCall(cudaDeviceSynchronize());
}

namespace pcl
{
namespace gpu
{
using namespace pcl::device;
using copy_fields_t = void (*)(int info[4], int size, const void* input, void* output);
} // namespace device
} // namespace pcl

PCL_EXPORTS void copyFieldsImpl(int in_size, int out_size, int rules[4], int size, const void* input, void* output)
{
pcl::device::copy_fields_t funcs[16][16] =
namespace pcl {
namespace gpu {
using namespace pcl::device;

PCL_EXPORTS void
copyFieldsImpl(
int in_size, int out_size, int rules[4], int size, const void* input, void* output)
{
// clang-format off
pcl::device::copy_fields_t funcs[16][16] =
{
{ /**/ cf<1,1>, cf<1, 2>, cf<1, 3>, cf<1, 4>, /**/ cf<1, 5>, cf<1, 6>, cf<1, 7>, cf<1, 8>, /**/ cf<1, 9>, cf<1,10>, cf<1, 11>, cf<1, 12>, /**/ cf<1, 13>, cf<1, 14>, cf<1, 15>, cf<1,16> },
{ /**/ cf<2,1>, cf<2, 2>, cf<2, 3>, cf<2, 4>, /**/ cf<2, 5>, cf<2, 6>, cf<2, 7>, cf<2, 8>, /**/ cf<2, 9>, cf<1,10>, cf<2, 11>, cf<2, 12>, /**/ cf<2, 13>, cf<2, 14>, cf<2, 15>, cf<2,16> },
Expand All @@ -112,9 +109,8 @@ namespace pcl
{ /**/ cf<15,1>, cf<15,2>, cf<15,3>, cf<15,4>, /**/ cf<15,5>, cf<15,6>, cf<15,7>, cf<15,8>, /**/ cf<15,9>, cf<1,10>, cf<15,11>, cf<15,12>, /**/ cf<15,13>, cf<15,14>, cf<15,15>, cf<15,16> },
{ /**/ cf<16,1>, cf<16,2>, cf<16,3>, cf<16,4>, /**/ cf<16,5>, cf<16,6>, cf<16,7>, cf<16,8>, /**/ cf<16,9>, cf<1,10>, cf<16,11>, cf<16,12>, /**/ cf<16,13>, cf<16,14>, cf<16,15>, cf<16,16> }
};

funcs[in_size-1][out_size-1](rules, size, input, output);
}
}
// clang-format on
funcs[in_size - 1][out_size - 1](rules, size, input, output);
}

} // namespace gpu
} // namespace pcl