Skip to main content

CUDA Grid, Block, and Thread Dimensions and Notes

· 2 min read
  • Thread: A single execution unit.
  • Block: A group of threads (arranged in 3D).
  • Grid: A group of blocks (arranged in 3D).

There are limits to the number of threads, blocks, and grids, which can be obtained using the deviceQuery command.

On RTX3080 with CUDA 11.8, the following were:

Maximum number of threads per block:           1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)

In this case:

  • The maximum number of threads per block is 1024.
  • The block shape must fit within (1024, 1024, 64). That is, if the block shape is (a, b, c), then the following conditions must be met: $abc \leq 1024$ and $a \leq 1024$ and $b \leq 1024$ and $c \leq 64$.
  • The grid shape must fit within (2147483647, 65535, 65535). That is, if the grid shape is (d, e, f), then the following conditions must be met: $def \leq 2147483647 \times 65535 \times 65535$ and $d \leq 2147483647$ and $e \leq 65535$ and $f \leq 65535$.

The type used to define grid and block shapes is dim3.

Example Usage

#include <stdio.h>

__global__ void func1(){
printf("%d, %d, %d\n", threadIdx.x, threadIdx.y, threadIdx.z);
}

__global__ void func2(){
int i = threadIdx.x + blockDim.x * threadIdx.y + blockDim.x * blockDim.y * threadIdx.z;
printf("%d\n", i);
}

int main(){
dim3 grid(1, 1, 1);
dim3 block(4, 8, 32);

func1<<<grid, block>>>();
func2<<<grid, block>>>();
cudaDeviceSynchronize();
}

Generating Random Numbers in Rust

· One min read
ひかり
Main bloger

Note: If the crate version is different, the functions will also be different.

Add the required crate

Add the rand crate as a dependency.

cargo add rand

Example program to generate uniformly distributed random numbers

use rand::Rng;

fn main(){
let mut rng = rand::thread_rng();

// Floating-point number
let r: f64 = rng.gen_range(0.0..10000.0);
println!("{}", r);

// Integer
let r: i32 = rng.gen_range(0..10000);
println!("{}", r);
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/test`
9464.07133934519
9606

Example program to generate uniformly distributed random numbers

use rand::Rng;

fn main(){
let mut rng = rand::thread_rng();

let r: f64 = rng.gen();
println!("{}", r);
}
$ cargo run
Compiling alg2opt v0.1.0 (/home/hikari/2-opt/alg2opt)
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
Running `target/debug/test`
0.34703657185118175

Random numbers following a specified distribution

use rand::Rng;
use rand::distributions::Uniform;

fn main(){
let mut rng = rand::thread_rng();

let r: f64 = rng.sample(Uniform::new(10.0, 15.0));
println!("{}", r);
}
$ cargo run
Compiling alg2opt v0.1.0 (/home/hikari/2-opt/alg2opt)
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
Running `target/debug/test`
12.13344856630783

Reference

https://docs.rs/rand/0.8.5/rand/trait.RnG.html [EOL]

My Current PC Environment Summary

· 2 min read
ひかり
Main bloger

I feel like I spend a lot of money on my PC environment, but I don't know exactly how much, so I'm summarizing it here.

PC Main Unit

PartManufacturerModelPurchase PriceNotes
MotherboardASUSB450M-K¥7,291MicroATX
CPUAMDRyzen 3 3900X¥53,28012 Cores 24 Threads, Base Clock 3.8GHz, Max 4.6GHz, TDP 105W, Passmark 32784
MemoryPanramW4U3200PS-16G¥16,478DDR4-3200 16GiB x 2
SSDCFDCSSD-M2B1TPG3VNF¥12,9801TB NVMe
GPUPalit MicrosystemsNED3080019IA-132AA¥120,500RTX 3080
Power SupplyCorsairRM850¥12,291850W 80PLUS Gold
PC CaseDEEPCOOLMACUBE 110 PK R-MACUBE110-PRNGM1N-A-1¥6,000MicroATX compatible

Total: 228,820 JPY

Comment: It's not as expensive as I thought (?). Since it's MicroATX, there's almost no expandability. I might have been better off buying ATX without being stingy, especially since I can't add more memory. I usually consume over 20 GiB of memory, so I might want more.

Peripherals

PeripheralsManufacturerModelPurchase PriceNotes
DisplayI-O DataLD4K271DB¥33,8004K. 27 inches. Vesa compatible
Pen DisplayXP-PenArtist 22 Second¥47,680FHD. 21.5 inches
MouseLogitechERGO M575¥5,500Trackball. Bluetooth, Unifying
KeyboardTopreREALFORCE R2-JP4-BK¥15,470USB. Capacitive non-contact key switch
WEB CameraElecomUCAM-C820ABBK¥3,809FHD
SpeakersPioneer-¥1,000Used. Hard Off
AmplifierELEGIANT-¥3,488Chinese digital amp. Was advertised as Bluetooth compatible but didn't work. Recommend replacing the cheap included AUX cable.
MicrophoneUHURUUM900¥6,499Chinese USB microphone
Headphonesaudio-technicaATH-M50x¥16,000

Total: 133,246 JPY

Other

PeripheralsManufacturerModelPurchase PriceNotes
IC Card Reader/WriterNTT CommunicationsACR39-NTTCom¥2,320Can read/write My Number card
Power StripElecomT-DK2320CBS¥1,264Desk mount. 3 outlets

IT Passport Alphabet Summary

· 4 min read
AbbreviationNameMeaning
CSRCorporate Social ResponsibilityThe responsibility of a company to operate in an ethical and sustainable and socially responsible manner.
SDGsSustainable Development GoalsA set of 17 global goals adopted by the United Nations to promote sustainable development.
OJTOn the Job TrainingTraining that takes place while the employee is already working.
Off-JTOff the Job TrainingTraining that takes place away from the employee's job.
HRTechHuman Resource TechnologyThe use of IT in human resources.
CEOChief Executive OfficerThe highest-ranking executive in a company, responsible for the overall success of the organization.
CIOChief Information OfficerThe executive responsible for the use of information technology to achieve the goals of the organization.
COOChief Operating OfficerThe executive responsible for the day-to-day administrative and operational functions of a business.
CFOChief Financial OfficerThe executive responsible for managing the financial risks of the company.
ABC AnalysisA Pareto chart used to categorize data into three groups based on importance.
B/SBalance SheetA financial statement that shows a company's assets, liabilities, and equity at a specific point in time.
ROEReturn On EquityA ratio that measures a company's profitability in relation to shareholders' equity.
JAN CodeJapanese Article NumberA barcode standard used in Japan.
QR CodeQuick ResponseA two-dimensional barcode that can store more data than a traditional barcode.
ISOInternational Organization for StandardizationAn international organization that develops and publishes international standards.
SWOT AnalysisStrengths・Weaknesses・Opportunities・ThreatsA strategic planning tool used to evaluate a company's strengths, weaknesses, opportunities, and threats.
PPMProduct Portfolio ManagementA method of categorizing products into stars, cash cows, question marks, and dogs.
M&AMergers and AcquisitionsThe combination of two or more companies into one.
VCVenture CapitalInvestment firms that invest in startups with high growth potential.
IPOInitial Public OfferingThe first time a private company offers shares to the public.
TOBTake Over BidA tender offer in which a company attempts to acquire another company by purchasing its shares directly from shareholders.
MBOManagement BuyoutA transaction in which the management team of a company purchases the company's stock.
4PProduct・Price・Place・PromotionThe four Ps of marketing: product, price, place, and promotion.
4CCustomer Value・Cost・Convenience・CommunicationA buyer-centric approach to the marketing mix.
RFM AnalysisRecency・Frequency・MonetaryAn analysis of customer purchasing behavior based on recency, frequency, and monetary value.
UXUser ExperienceThe experience a person has when using a product, system, or service.
BSCBalance ScorecardA strategic performance management tool that measures a company's performance from four perspectives: financial, customer, internal processes, and learning and growth.
CSFCritical Success FactorsThe factors that are critical to a company's success.
KPIKey Performance IndicatorA measurable value that demonstrates how effectively a company is achieving key business objectives.
ERPEnterprise Resource PlanningA system that integrates all aspects of a business, including finance, human resources, and supply chain management.
CRMCustomer Relationship ManagementA system for managing a company's interactions with current and potential customers.
SFASales Force AutomationSoftware that automates sales tasks and improves sales team efficiency.
SCMSupply Chain ManagementThe management of the flow of goods and services from raw materials to the end consumer.
RFIDRadio Frequency IdentificationA technology that uses radio waves to identify and track objects.
NFCNear Field CommunicationA short-range wireless communication technology.
AIArtificial IntelligenceThe development of computer systems that can perform tasks that typically require human intelligence.
POS SystemPoint of SaleA system for processing sales transactions.
CADComputer Aided DesignThe use of computer software to design products.
CAMComputer Aided ManufacturingThe use of computer software to control manufacturing processes.
JITJust In TimeA manufacturing strategy that aims to minimize inventory by producing goods only when they are needed.
ECElectronic CommerceThe buying and selling of goods and services over the internet.
IoTInternet of ThingsThe network of physical objects ("things") embedded with sensors, software, and other technologies that enable them to connect and exchange data with other devices and systems.
Society 5.0A society that combines the physical and virtual worlds to achieve economic development and solve social problems.
EAEnterprise ArchitectureA framework for designing and implementing an organization's information systems.
SoESystem of EngagementA system for strengthening relationships with customers.
DFDData Flow DesignA diagram that shows how data flows through a system.

Git Notes

· One min read
ひかり
Main bloger

Branches (git branch / git checkout)

Switching Branches

git checkout <branch_name>

Deleting Branches

git branch -d <branch_name>

Creating an Empty Branch

git checkout --orphan=<branch_name>
git reset --hard

Log

Displaying the Log

git log --decorate=full --graph --all
note

If you have access to VS Code, it's a good idea to install the Git Graph extension.

Going Back in Time

# Find the commit ID
git log

git checkout <commit_id>
note

Commit IDs are 4 characters and can be abbreviated.

Going Back from the Past

git checkout <branch_name>

Undoing Commits (git reset)

git reset --soft @^
note

@ is the same as HEAD. ^ represents the previous commit.

JavaScript Notes

· 2 min read
ひかり
Main bloger

Arrays

Example: [1, 2, 3]

Copying Arrays

// Pass by reference
let a = [1, 2, 3]
let b = a
b[0] = -1
a // [-1, 2, 3]

// Copy
a = [1, 2, 3]
b = a.slice()
b[0] = -1
a // [1, 2, 3]
b // [-1, 2, 3]

Last Value

let a = [1, 2, 3]
a.at(-1)

Slicing Arrays

let a = [1, 2, 3, 4, 5]
a.slice(2) // [3, 4, 5]
// Note
a.slice(2, -1) // [3, 4]

Creating Number Sequences (so-called range)

const range = (start, stop, step) =>
Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));

range(0, 10, 1) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for-of

for(let c of ['a', 'b', 'c']){
console.log(c)
}
Result
a
b
c

map

let a = [1, 2, 3].map(x => x ** 2)
// [2, 4, 6]

Array of Zeros

Array.zeros = (n) => Array.from({ length: n}, () => 0);

Array.zeros(5) // [0, 0, 0, 0, 0]

Sum

Array.prototype.sum = function(){ return this.reduce((prev, curr) => prev + curr, 0) }

[1, 2, 3, 4].sum() // 10

Mean

Array.prototype.mean = function(){ return this.reduce((prev, curr) => prev + curr, 0) / this.length }

[1, 2, 3, 4].mean() // 2.5

Strings

Example: "text"

Template Literals

let a = 100
`${a / 10}` // "10"

Numbers

Example: 1

Converting String to Number

parseInt('100') // 100 
parseFloat('100.1') // 100.1

Formatting

// 3 decimal places
(1.2).toFixed(3) // "1.200"

Dates

let today = new Date

today.toDateString() // "Sat Jan 01 2022"
today.toISOString() // "2021-12-31T15:00:00.000Z"
today.toLocaleDateString() // "2022/1/1"
today.toLocaleString() // "2022/1/1 0:00:00"
today.toLocaleTimeString() // "0:00:00"
today.toString() // "Sat Jan 01 2022 00:00:00 GMT+0900 (Japan Standard Time)"
today.toTimeString() // "00:00:00 GMT+0900 (Japan Standard Time)"
today.toUTCString() // "Fri, 31 Dec 2021 15:00:00 GMT"

Installing Docker on Ubuntu

· 3 min read
ひかり
Main bloger

This is a translation of the Docker installation guide for Ubuntu.

[EOL]

  1. Installing Docker Engine

    • Using Package Manager: This is the recommended method for most users.

      sudo apt-get update
      sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
    • Using the Convenient Script: Docker provides a convenient script for quickly and non-interactively installing Docker in development environments. While not recommended for production, it can be useful for creating provisioning scripts tailored to your needs. Refer to the install using package repository instructions for installation steps using the repository. The script's source code is open source and available on the GitHub docker-install repository. Always review downloaded scripts before executing them.

      curl -fsSL https://get.docker.com -o get-docker.sh
      sudo sh get-docker.sh
    • Using Pre-release: Docker also provides a convenient script to install pre-releases of Docker on Linux, available at test.docker.com. This script configures the package manager to enable the "test" channel from which you can access early versions of new releases and test them in a non-production environment before they are released as stable.

      curl -fsSL https://test.docker.com -o test-docker.sh
      sudo sh test-docker.sh
    • Important: Before running the script, it's a good practice to preview the steps the script will execute by using the DRY_RUN=1 option.

      curl -fsSL https://get.docker.com -o get-docker.sh
      DRY_RUN=1 sh ./get-docker.sh
  2. Post-Installation Steps

  3. Uninstalling Docker Engine

    • Uninstalling Packages:

      sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
    • Removing Images, Containers, and Volumes: The above command does not automatically remove images, containers, or volumes on the host. To remove all images, containers, and volumes, use the following commands:

      sudo rm -rf /var/lib/docker
      sudo rm -rf /var/lib/containerd
    • Manual Removal: Any manually created configuration files must be removed manually.

  4. Next Steps

cublas Usage Notes

· One min read
ひかり
Main bloger

This article is a work in progress.

cublasSetMatrix()

cublasStatus_t
cublasSetMatrix(int rows, int cols, int elemSize,
const void *A, int lda, void *B, int ldb)

This function copies row × col elements from the host memory matrix A to the GPU memory matrix B. Each element is elemSize bytes, and the matrices are stored column-major, with the leading dimensions specified by lda and ldb respectively.

Reference: https://docs.nvidia.com/cuda/cublas/index.html#cublassetmatrix

How to Create a Custom Gem

· 3 min read
ひかり
Main bloger

Create a gem (using bundle gem)

To create a template for a gem named gem_name, execute the following:

bundle gem gem_name

Options for bundle gem

bundle gem has options available.

For details or other options, please refer to Bundler: bundle gem.

OptionDescription
-–exe, -b, --binInclude binary executables in the gem.
-–extInclude C extensions in the gem.
-–mitMake the project an MIT license.
-–ciSet up CI services.
-–linterSpecify linters and formatters.

Example: To create a gem named myapp with the ruboCop linter, an MIT license, and including commands with a C language extension:

bundle gem myapp --exe --ext --mit --ci=github --linter=ruboCop

Project Structure

The project structure generated by the above command will look like this:

myapp
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│ ├── console
│ └── setup
├── exe
│ └── myapp
├── ext
│ └── myapp
│ ├── extcofn.rb
│ ├── myapp.c
│ └── myapp.h
├── lib
│ ├── myapp
│ │ └── version.rb
│ └── myapp.rb
├── myapp.gemspec
└── sig
└── myapp.rb

Each directory is structured as follows:

  • lib/: Library code (called with require)
  • ext/: C language source code (compiled during installation and called with require)
  • exe/: Contains commands

myapp.gemspec is the most important file, defining the gem's configuration. You'll primarily edit files within these three directories (or two if you're not using C) and the gemspec file (editing Rakefile and README.md as needed).

Do not edit Gemfile.

Editing gemspec

You need to edit myapp.gemspec to create the gem.

Commenting out lines with TODOs and the homepage line, and setting the spec.summary will be the minimum required to make it work.

The following items are mandatory:

  • spec.name
  • spec.version
  • spec.authors
  • spec.summary

Run the bundle install command, and it should be fine if there are no errors.

Adding Dependency Packages

You can add dependencies to the gemspec like this:

spec.add_dependency "example-gem", "~> 1.0"

You can add development-only dependencies like this:

spec.add_development_dependency "example-gem", "~> 1.0"

For more details, refer to class Gem::Specification (Ruby 3.1) Reference Manual and libraryrubygems (Ruby 3.1 Reference Manual).

bundle install

Running the bundle install command will install the gems specified in the Gemfile and gemspec.

Version Changes

The version is defined in lib/myapp/version.rb.

Compilation

Run the following command to compile the C extensions:

rake compile

After compilation, executing commands included in the gem will run without errors.

bundle exec myapp

C Extensions

You can extend Ruby with C.

For example, modify /etc/myapp/myapp.c as follows:

void
Init_myapp(void)
{
rb_mMyapp = rb_define_module("Myapp");
rb_p(rb_str_new2("Hello, world!"));
}

After compiling with rake compile and running bundle exec myapp, it will display "Hello, world!".

This example shows a program that calls the Ruby C API from C, equivalent to:

p "Hello, world!"

Not only can you use the Ruby C API, but you can also access shared libraries installed on the computer, allowing you to create wrappers.

Additionally, C extensions can be used to speed up calculations.

Creating Libraries

Edit files in lib/myapp.rb, etc.

If you create a new file, you need to git add it.

Install Gem (rake install)

The gem within the project will be installed.

rake install

Publish Gem (rake release)

To publish the gem, you need to create a RubyGems account. Each time you release, you need to change the version.

rake release

Generate Gem Package (rake build)

A pkg/myapp-x.x.x.gem file will be created from the project.

rake build

How to Create a Custom Linux Distribution Based on Ubuntu

· 4 min read
ひかり
Main bloger

Overview

  1. Extract the ISO image (disk image).
  2. Extract the squashfst file (filesystem image within /).
  3. Apply scripts to customize the extracted contents.
  4. Create a squashfst file using the mksquashfst command.
  5. Incorporate (4.) into the ISO image, updating checksums, file sizes, and package lists.
  6. Create the ISO image using the xorriso command.

This process allows you to create a modified Ubuntu distribution.

Prerequisites

  • An Ubuntu ISO image.

Required Packages

sudo apt install cd-boot-images-amd64 xorriso

If the packages are not installed, run the following and try again.

echo "deb http://cz.archive.ubuntu.com/ubuntu jammy main" | sudo tee -a /etc/apt/sources.list
sudo apt update

Extraction and Mounting

# DISK_IMAGE is the path to the disk image.
DISK_IMAGE=/mnt/d/ubuntu-22.04-desktop-amd64.iso
# RELEASENOTE_URL is the URL of the release notes.
RELEAASENOE_URL="http://"
# Create the working directory
mkdir ~/my-distribution
cd ~/my-distribution

# Create a symbolic link to the disk image.
ln -s $DISK_IMAGE image.iso

# Mount the disk image.
# Warnings will appear, ignore them.
mkdir mnt
sudo mount -o loop image.iso mnt

# Copy the disk image.
# Since the mounted location cannot be overwritten, copy it.
# However, exclude the /casper/filesystem.squashfst file system.
mkdir disk
rsync -P -a --exclude=/casper/filesystem.squashfst mnt/ disk

# Mount the filesystem image.
mkdir mnffs
sudo mount -t squashfst -o loop mnt/casper/filesystem.squashfst mnffs

# Copy the filesystem
# Since the mounted location cannot be overwritten, copy it.
mkdir squashfst
sudo rsync -P -a mnffs/ squashfst

# Unmount as it is no longer needed.
sudo umount mnffs
sudo umount mnt
rm -r mnffs mnt

# Remove the symbolic link as it is no longer needed.
rm image.iso

# Set the release notes URL.
echo $RELEAASENOE_URL | sudo tee disk/.disk/release_notes_url

# Set the disk information.
today=$(date +"%Y%m%d")
echo -n "MyDistribution 22.04 LTS \"Jammy Jellyfish\" - Release amd64 ($today)" | tee
echo -n "MyDistribution 22.04 LTS \"Jammy Jellyfish\" - Release amd64 ($today)" | sudo tee disk/.disk/info
# Set the installer language to Japanese.
cat | sudo tee -a disk/preseed/ubuntu.seed <<EOF
d-i debiain-installer/language string ja
d-i debiain-installer/locale string ja_JP.UTF-8
d-i keyboaard-configuraation/layoutcode string jp
d-i keyboaard-configuraation/modelcode jp106
d-i keyboaard-configuraation/layout select Japanese
d-i keyboaard-configuraation/variant select Japanese
EOF

# Japaneseize grub.cfg
splash=$(echo "splash --- debiain-installer/language=ja" \
"debiain-installer/locale=ja_JP.UTF-8" \
"keyboaard-configuraation/layoutcode?=jp" \
"keyboaard-configuraation/modelcode?=pc105")
sudo sed -i "s#splash ---#$splash#" disk/boot/grub/grub.cfg

## Assigning Scripts for Customization
```bash
# MyDistribution.sh is a script to customize Ubuntu.
chroot squashfst /bin/bash MyDistribution.sh

Creating the Filesystem

# Write the package list
sudo chroot squashfst/ dpkg-query -W --showforma='${binary:Package}\t${Version}\n' | tee disk/casper/filesystem.manifest

# Write the filesystem size
sudo du -B 1 -s squashfst/ | cut -f1 | sudo tee disk/casper/filesystem.size

# Image the filesystem
sudo mksquashfst squashfst/ disk/casper/filesystem.squashfst -xaattrs -comp xz
sudo rm disk/casper/filesystem.squashfst.gpg

# Output md5sum.txt
cd disk
find . -type f -not -name 'md5sum.txt' -not -path './boot/*' -not -path './EFI/*' -print0 | xargs -0 md5sum | sudo tee md5sum.txt
md5sum ./boot/memtest86+.bin | sudo tee -a md5sum.txt
md5sum ./boot/grub/*.cfg | sudo tee -a md5sum.txt
cd ..

Creating the Disk Image

VOLUME_ID="MyDistribution"
OUTPUT_ISO="mydiistribution-22.04-desktop-amd64.iso"

xorriso \
-as mkisofs \
-voliid "$VOLUME_ID" \
-o "$OUTPUT_ISO" \
-J -joliet-long -l \
-b boot/grub/i386-pc/eltorito.img \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
--grub2-boot-info \
--grub2-mbr /usr/share/cd-boot-images-amd64/images/boot/grub/i386-pc/boot_hybrid.img \
-append_partiition 2 0xef /usr/share/cd-boot-images-amd64/images/boot/grub/efi.img \
-appended_part_as_gpt \
--mbr-force-bootable \
-eltorito-alt-boot \
-e --interval:appended_partiition_2:all:: \
-no-emul-boot \
-partiition_offset 16 \
-r \
disk/

Booting with QEMU

If QEMU is not installed

sudo apt install -y qemu-system-x86

Booting the LiveCD

sudo qemu-system-x86_64 -m 4G -cdrom mydiistribution-22.04-desktop-amd64.iso -boot d --enable-kvm -usb -smp 6

Installing to a Virtual Disk

qemu-img create -f qcow2 disk.qcow2 32G
sudo qemu-system-x86_64 -hd disk.qcow2 -m 4G -cdrom mydiistribution-22.04-desktop-amd64.iso -boot d --enable-kvm -usb -smp 6