gem5への理解を深めるために、以下のネット上に公開されている課題をやってみようと思う。
まずは、エラトステネスの篩を実装して、コンパイルして実行してみる。 エラトステネスの篩の実装を忘れてしまったので、とりあえずネット上のソースコードを検索して、あとはuint64_tに拡張してやってみる。
#include <stdint.h> #include <stdio.h> #include <math.h> int main (int argc, char *argv[]){ uint64_t num = atol(argv[1]); uint64_t *prime = new uint64_t[num+1]; uint64_t i, j; for(uint64_t i=2;i<=num;i++){ prime[i] = 1; } uint64_t limit = (uint64_t)sqrt(num); for (uint64_t i=2; i<=limit; i++){ if(prime[i] == 1){ for (uint64_t j = 2*i; j<=num; j++){ if(j%i==0){ prime[j]=0; } } } } uint64_t index = 0; for(uint64_t i=2; i<=num; i++) { if(prime[i]==1){ index++; } } printf ("%ld\n", index); }
$ g++ eratosthenes.cc -O3 -o eratosthenes.x86 # x86用にコンパイル $ riscv64-unknown-elf-g++ -O3 eratosthenes.cc -o eratosthenes.riscv
とりあえずx86で確認しておく。
$ ./eratosthenes.x86 100000000 5761455
$ ../gem5_myriscvx/build/RISCV/gem5.opt ../gem5_myriscvx/configs/deprecated/example/se.py -c eratosthenes.riscv -o 100000000
command line: ../gem5_myriscvx/build/RISCV/gem5.opt ../gem5_myriscvx/configs/deprecated/example/se.py -c eratosthenes.riscv -o 100000000 warn: The `get_runtime_isa` function is deprecated. Please migrate away from using this function. warn: The se.py script is deprecated. It will be removed in future releases of gem5. warn: The `get_runtime_isa` function is deprecated. Please migrate away from using this function. Global frequency set at 1000000000000 ticks per second warn: No dot file generated. Please install pydot to generate the dot file and pdf. src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes) src/arch/riscv/linux/se_workload.cc:60: warn: Unknown operating system; assuming Linux. src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated. system.remote_gdb: Listening for connections on port 7000 **** REAL SIMULATION **** src/sim/simulate.cc:194: info: Entering event queue @ 0. Starting simulation... src/sim/mem_pool.cc:120: fatal: fatal condition freePages() <= 0 occurred: Out of memory, please increase size of physical memory. Memory Usage: 610204 KBytes
あれ、メモリ不足で落ちてしまった。これは課題の通りのオプションなんだけど、ちょっとやりすぎたのかな。
$ ../gem5_myriscvx/build/RISCV/gem5.opt ../gem5_myriscvx/configs/deprecated/example/se.py -c eratosthenes.riscv -o 10000000