Skip to content
Merged
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
52 changes: 26 additions & 26 deletions CalcPi.jl/README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
# CalcPi.jl

JuliaからRustで実装されたMonte Carlo法によるπ計算ライブラリ(`calcpi-rs`)を呼び出すためのJuliaパッケージです。
A Julia package for calling a Rust-implemented Monte Carlo π calculation library (`calcpi-rs`) from Julia.

## 概要
## Overview

`CalcPi.jl`は、`calcpi-rs`ライブラリへのJuliaバインディングを提供します。Monte Carlo法を使用してπの値を計算できます。
`CalcPi.jl` provides Julia bindings to the `calcpi-rs` library. You can calculate the value of π using the Monte Carlo method.

## インストール
## Installation

```julia
using Pkg
Pkg.add(url="path/to/CalcPi.jl")
```

または、開発モードでインストール:
Or install in development mode:

```julia
using Pkg
Pkg.develop(path="path/to/CalcPi.jl")
```

## ビルド
## Building

パッケージをビルドすると、Rustライブラリが自動的にコンパイルされ、C APIバインディングが生成されます:
Building the package will automatically compile the Rust library and generate C API bindings:

```julia
using Pkg
Pkg.build("CalcPi")
```

## 使い方
## Usage

```julia
using CalcPi

# MonteCarloPiインスタンスを作成
# Create a MonteCarloPi instance
calc = CalcPi.MonteCarloPi()

# 100万サンプルでπを計算
# Calculate π with 1 million samples
result = CalcPi.calculate(calc, UInt64(1_000_000))
println("π ≈ $result")

# リソースを解放
# Release resources
CalcPi.release(calc)
```

## プロジェクト構造
## Project Structure

```
CalcPi.jl/
├── deps/
│ ├── build.jl # ビルドスクリプト
│ └── libcalcpi_rs.* # ビルドされたライブラリ(自動生成)
│ ├── build.jl # Build script
│ └── libcalcpi_rs.* # Built library (auto-generated)
├── src/
│ ├── CalcPi.jl # 高レベルAPI
│ └── C_API.jl # C APIバインディング(自動生成)
├── test/ # テストファイル
└── utils/ # C API生成ユーティリティ
│ ├── CalcPi.jl # High-level API
│ └── C_API.jl # C API bindings (auto-generated)
├── test/ # Test files
└── utils/ # C API generation utilities
```

## 開発
## Development

開発ガイドについては`DEVELOPMENT.md`を参照してください。
For development guide, see `DEVELOPMENT.md`.

## テスト
## Testing

```julia
using Pkg
Pkg.test("CalcPi")
```

## 依存関係
## Dependencies

- `CEnum.jl` - C enumのサポート
- `RustToolChain.jl` - Rust toolchainの管理(ビルド時のみ)
- `CEnum.jl` - C enum support
- `RustToolChain.jl` - Rust toolchain management (build-time only)

## ライセンス
## License

このプロジェクトのライセンス情報については、ルートディレクトリのREADMEを参照してください。
For license information about this project, please refer to the README in the root directory.
16 changes: 8 additions & 8 deletions CalcPi.jl/src/CalcPi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export calcpi_monte_carlo_pi_reset
"""
MonteCarloPi

モンテカルロ法でπを計算する計算機の高レベルAPIラッパーです。
High-level API wrapper for a calculator that computes π using the Monte Carlo method.
"""
mutable struct MonteCarloPi
ptr::Ptr{calcpi_monte_carlo_pi}
Expand All @@ -38,7 +38,7 @@ end
"""
release(calc::MonteCarloPi)

計算機のリソースを解放します。通常は自動的に呼ばれますが、明示的に呼ぶこともできます。
Releases the calculator's resources. Usually called automatically, but can be called explicitly.
"""
function release(calc::MonteCarloPi)
if calc.ptr != C_NULL
Expand All @@ -50,7 +50,7 @@ end
"""
calculate(calc::MonteCarloPi, samples::UInt64) -> Float64

指定された数のサンプルを生成してπを推定します。
Generates the specified number of samples and estimates π.
"""
function calculate(calc::MonteCarloPi, samples::UInt64)
if calc.ptr == C_NULL
Expand All @@ -72,7 +72,7 @@ end
"""
estimate(calc::MonteCarloPi) -> Float64

現在のサンプルからπの推定値を取得します。
Gets the π estimate from the current samples.
"""
function estimate(calc::MonteCarloPi)
if calc.ptr == C_NULL
Expand All @@ -94,7 +94,7 @@ end
"""
total_samples(calc::MonteCarloPi) -> UInt64

総サンプル数を取得します。
Gets the total number of samples.
"""
function total_samples(calc::MonteCarloPi)
if calc.ptr == C_NULL
Expand All @@ -116,7 +116,7 @@ end
"""
inside_circle(calc::MonteCarloPi) -> UInt64

円内の点数を取得します。
Gets the number of points inside the circle.
"""
function inside_circle(calc::MonteCarloPi)
if calc.ptr == C_NULL
Expand All @@ -138,7 +138,7 @@ end
"""
reset(calc::MonteCarloPi)

統計情報をリセットします。
Resets the statistics.
"""
function reset(calc::MonteCarloPi)
if calc.ptr == C_NULL
Expand All @@ -157,7 +157,7 @@ end
"""
calc_pi(samples::UInt64) -> Float64

簡易的なπ計算関数。内部でMonteCarloPiを作成・使用・解放します。
Simple π calculation function. Internally creates, uses, and releases a MonteCarloPi instance.
"""
function calc_pi(samples::UInt64)
calc = MonteCarloPi()
Expand Down
86 changes: 41 additions & 45 deletions CalcPi.jl/utils/README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
# CalcPi.jl C-API 自動生成ツール
# CalcPi.jl C-API Auto-generation Tool

このディレクトリには、CヘッダーファイルからJuliaバインディングを自動生成するツールが含まれています。
This directory contains tools for automatically generating Julia bindings from C header files.

## 概要
## Overview

`SparseIR.jl/utils`の仕組みを参考に、`calcpi.h`からJuliaの`C_API.jl`を自動生成します。
Based on the mechanism from `SparseIR.jl/utils`, this tool automatically generates Julia's `C_API.jl` from `calcpi.h`.

## ファイル構成
## File Structure

- `generate_C_API.jl` - メインの生成スクリプト
- `generator.toml` - Clang.jlの設定ファイル
- `prologue.jl` - 生成コードのプロローグ(ライブラリロード処理)
- `Project.toml` - 依存関係(Clang.jl
- `generate_C_API.jl` - Main generation script
- `generator.toml` - Clang.jl configuration file
- `prologue.jl` - Prologue for generated code (library loading process)
- `Project.toml` - Dependencies (Clang.jl)

## 使用方法
## Usage

### 1. Rustライブラリのビルド
### 1. Building the Rust Library

まず、Rustライブラリをビルドしてヘッダーファイルを生成します:
First, build the Rust library to generate the header file:

```bash
cd ../../calcpi-rs
cargo build --release
```

これにより、`calcpi-rs/include/calcpi.h`が生成されます。
This generates `calcpi-rs/include/calcpi.h`.

### 2. Juliaバインディングの生成
### 2. Generating Julia Bindings

生成スクリプトを実行します:
Run the generation script:

```bash
cd CalcPi.jl/utils
julia generate_C_API.jl
```

デフォルトでは、`../../calcpi-rs`を探します。別のパスを指定する場合:
By default, it looks for `../../calcpi-rs`. To specify a different path:

```bash
julia generate_C_API.jl --calcpi-rs-dir /path/to/calcpi-rs
```

### 3. 生成されるファイル
### 3. Generated Files

`CalcPi.jl/src/C_API.jl`が生成されます。このファイルには:
`CalcPi.jl/src/C_API.jl` is generated. This file contains:

- 型定義(`calcpi_monte_carlo_pi`など)
- 定数定義(`CALCPI_SUCCESS`など)
- C関数のラッパー(`calcpi_monte_carlo_pi_new`など)
- Type definitions (e.g., `calcpi_monte_carlo_pi`)
- Constant definitions (e.g., `CALCPI_SUCCESS`)
- C function wrappers (e.g., `calcpi_monte_carlo_pi_new`)

が含まれます。
## Generation Script Behavior

## 生成スクリプトの動作
1. **Command-line argument parsing**: Path can be specified with `--calcpi-rs-dir`
2. **Directory validation**: Checks for the existence of `calcpi-rs/include/calcpi.h`
3. **Parsing with Clang.jl**: Parses the C header
4. **Julia code generation**: Generates `C_API.jl`
5. **Prologue insertion**: Adds the contents of `prologue.jl` at the beginning

1. **コマンドライン引数の解析**: `--calcpi-rs-dir`でパスを指定可能
2. **ディレクトリの検証**: `calcpi-rs/include/calcpi.h`の存在確認
3. **Clang.jlによるパース**: Cヘッダーを解析
4. **Juliaコードの生成**: `C_API.jl`を生成
5. **プロローグの挿入**: `prologue.jl`の内容を先頭に追加

## 設定のカスタマイズ
## Configuration Customization

### generator.toml

Expand All @@ -75,33 +73,31 @@ extract_c_comment_style = "doxygen"

### prologue.jl

ライブラリのロード処理を定義します。ローカルビルドを優先し、なければシステムライブラリを探します。
Defines the library loading process. It prioritizes local builds and falls back to system libraries if not found.

## トラブルシューティング
## Troubleshooting

### エラー: calcpi.h not found
### Error: calcpi.h not found

Rustライブラリをビルドしてください:
Build the Rust library:

```bash
cd calcpi-rs && cargo build --release
```

### エラー: CEnum not found

生成された`C_API.jl`で`CEnum`が使われていない場合は、`using CEnum`の行を削除してください。
### Error: CEnum not found

### 生成コードの修正
If `CEnum` is not used in the generated `C_API.jl`, remove the `using CEnum` line.

生成されたコードは自動生成なので、直接編集せずに:
### Modifying Generated Code

1. Cヘッダーを修正して再生成
2. `prologue.jl`を修正して再生成
3. `generator.toml`の設定を変更して再生成
Since the generated code is auto-generated, do not edit it directly. Instead:

してください。
1. Modify the C header and regenerate
2. Modify `prologue.jl` and regenerate
3. Change the settings in `generator.toml` and regenerate

## 参考
## References

- `SparseIR.jl/utils/generate_C_API.jl` - 参考実装
- `SparseIR.jl/utils/generate_C_API.jl` - Reference implementation
- [Clang.jl Documentation](https://github.com/JuliaInterop/Clang.jl)
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# RustToolChainExamples.jl

このリポジトリは、JuliaからRustコードを呼び出す方法を示すサンプルプロジェクトのコレクションです。
This repository is a collection of sample projects demonstrating how to call Rust code from Julia.

## プロジェクト構成
## Project Structure

### メインプロジェクト
### Main Projects

- **`calcpi-rs/`** - Rustで実装されたMonte Carlo法によるπ計算ライブラリ
- **`CalcPi.jl/`** - `calcpi-rs`をJuliaから呼び出すためのJuliaパッケージ
- **`calcpi-rs/`** - Rust library implementing π calculation using the Monte Carlo method
- **`CalcPi.jl/`** - Julia package for calling `calcpi-rs` from Julia

### サンプルプロジェクト
### Sample Projects

- **`cargo_example/`** - Cargoを使ったRustプロジェクトの例
- **`rustc_example/`** - rustcを直接使ったRustプロジェクトの例
- **`cargo_example/`** - Example of a Rust project using Cargo
- **`rustc_example/`** - Example of a Rust project using rustc directly

## セットアップ
## Setup

### 前提条件
### Prerequisites

- Julia 1.6以上
- Julia 1.10 or later
- Rust toolchain (rustc, cargo)
- [RustToolChain.jl](https://github.com/AtelierArith/RustToolChain.jl) パッケージ
- [RustToolChain.jl](https://github.com/AtelierArith/RustToolChain.jl) package

### インストール
### Installation

```julia
using Pkg
Pkg.add("RustToolChain")
```

## 使い方
## Usage

各サンプルプロジェクトのREADMEを参照してください。
Please refer to the README of each sample project.

## ライセンス
## License

このプロジェクトのライセンス情報については、各サブディレクトリのREADMEを参照してください。
For license information about this project, please refer to the README in each subdirectory.
Loading