xsimd icon indicating copy to clipboard operation
xsimd copied to clipboard

Support RISC-V SIMD/Vector extension

Open davidbrochart opened this issue 2 years ago • 2 comments

It could be interesting to support RISC-V SIMD or Vector extension. The latter looks very promising as the vector length and data type can be changed at run-time, and is generally considered more efficient than packed-SIMD.

davidbrochart avatar Jul 12 '21 08:07 davidbrochart

I guess we could start by using Spike, a behavioral RISC-V simulator that supports the V extension.

davidbrochart avatar Jul 19 '21 20:07 davidbrochart

Renode also supports the V extension now.

davidbrochart avatar Dec 07 '21 16:12 davidbrochart

I am interested in collaborating on this. Has any work been done?

pavitrar20 avatar Oct 11 '22 05:10 pavitrar20

Not yet, your contribution would be very welcome!

davidbrochart avatar Oct 11 '22 06:10 davidbrochart

I appreciate pointers on how to get started. I am more of a firmware person- but am interested in using RISC-V SIMD extension for an application

pavitrar20 avatar Oct 11 '22 06:10 pavitrar20

Maybe run an xtensor program on Spike or Renode?

davidbrochart avatar Oct 11 '22 07:10 davidbrochart

Has anyone run an xtensor program on Spike?

I am able to independently compile a hello program with riscv64-unknown-elf-gcc and use spike pk hello to display

And also use G++ with C++14 to compile xtensor and xtl first program(given in the xtensor page) and run it like a g++ program. But, Spike would not recognize this as a loader file when just compiled with g++.

The platform - Apple MAC M2

pavitrar20 avatar Oct 17 '22 18:10 pavitrar20

Has anyone run an xtensor program on Spike?

I think you're the first one to attempt running xtensor on a RISC-V simulator @pavitrar20 !

I am able to independently compile a hello program with riscv64-unknown-elf-gcc and use spike pk hello to display

I think you need to do the same with xtensor. Compiling xtensor with g++ on your (real) machine and running it in the RISC-V simulator won't work. A simple xtensor program doesn't link with any external library, so you should have all the sources to compile it for RISC-V. What issue are you seeing?

davidbrochart avatar Oct 17 '22 19:10 davidbrochart

  1. I tried it in ubuntu 20.04 LTS . riscv64-unknown-elf-gcc seems to have some documented issues

Basicallly, Ubuntu has binutils and gcc packages for riscv64-unknown-elf, but no libraries. A simple program hello cannot really compile with riscv64-unknown-elf-gcc but can be with just gcc.!

  1. I tried on Apple MAC M2 based platform Here a simple compilation works - Work % riscv64-unknown-elf-gcc hello.c -o hello
    Work % spike pk hello bbl loader Hello RISC-V ISA Simulator!!

But a riscv64-unknown-elf-g++ with xtensor did not build when xtensor and xtl were compiled from source with cmake but in a directory /tmp/build.

Work % riscv64-unknown-elf-g++ --std=c++14 -I /tmp/build/xtensor -I /tmp/build/xtl firstTest.cpp -o example firstTest.cpp:2:10: fatal error: xtensor/xarray.hpp: No such file or directory 2 | #include <xtensor/xarray.hpp> | ^~~~~~~~~~~~~~~~~~~~ compilation terminated.

My thoughts are to build xtensor and xtl in the same directory at the same level. And try.

I compiled and built xtensor and xtl as follows- cd /tmp/build cmake -DCMAKE_INSTALL_PREFIX=/usr/local . cd /tmp/build/xtl make install

As you see I have all the sources. Does the above compilation steps amount to compiling for RISCV?

pavitrar20 avatar Oct 18 '22 05:10 pavitrar20

Thanks @pavitrar20 for giving it a try. Please note though that there's no need to go through xtensor to implement xsimd support ;-) If you don't enable XTL complex (which is probably better for starters) this should be as simple as creating a sample program, say:

#include <xsimd/xsimd.hpp>
#include <iostream>
int main() {
   xsimd::batch<float> x(1.f), y(2.f);
   sdt::cout << (x + y) << std::endl;
   return 0;
}

compiled with riscv64-unknown-elf-g++ --std=c++14 -Iinclude test.cpp -o test assuming you're in xsimd source. This should fail on an architecture check :-)

serge-sans-paille avatar Oct 18 '22 05:10 serge-sans-paille

Yes, It did fail on Architecture check

pavitrar20 avatar Oct 18 '22 06:10 pavitrar20

Updating status- while compilation succeeds in xsimd lib as well as for other generic tests , using "pk" is inconsistent- This is where I am at - spike []/pk test.cpp for example either leads to a silent hang or a verbose failure (bad syscall). There are open issues of several types. Trying to understand this and resolve at the moment.

[](url) ](https://github.com/riscv-software-src/riscv-tools/issues/324)

[[[]](https://github.com/riscv-software-src/riscv-tests/issues/207)]())(

Tried compiling for 32 bit variants but pk displays error [[]](https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1087)(url)

Andrew Waterman recommends not to use pk and to directlty run on spike but it is not clear how to print messages without pk (a JTAG emulator is not available)

pavitrar20 avatar Nov 14 '22 18:11 pavitrar20

Thanks for the update @pavitrar20, looks like you're making progress. Another option would be to simulate an RTL model of a RISC-V SoC, but I don't know if there are open-source CPUs that implement the vector extension. Then you could directly inspect internal signals of the CPU and see if things are indeed executed in parallel. But that would probably be a bigger work.

davidbrochart avatar Nov 14 '22 20:11 davidbrochart

Hang on! I may have access to a vector CPU soon for some other work. It can also be repurposed for this. Perhaps inspection could happen through JTAG- I will update on this as soon as I know more.

pavitrar20 avatar Nov 15 '22 22:11 pavitrar20

Hello, I'm currently working on upstreaming a port at https://github.com/rivosinc/xsimd/tree/upstream-rvv. I'll open a PR as soon as I have something (expecting by end of month).

I'm running into 1 test failure, and I'll want to simplify it a bit before.

luhenry avatar Nov 17 '23 16:11 luhenry