10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MATLABをColab上で実行する方法

Last updated at Posted at 2025-08-12

1. はじめに

この記事では以下のようにmatlabコードをcolab上で実行することを目指します。ページの最後に今回の記事で用いたコード全体を載せていますので、ぜひご活用ください。
image.png

2. 実行環境のセットアップ

はじめに、Colabのノートブック上でMATLABを実行するための環境を整えます。ColabではGPUを無料で利用できるため、GPU実行を手軽に試すことができます。

2.1. MATLABのインストール

はじめに、MATLAB本体およびGPU計算に必要な「Parallel Computing Toolbox」をインストールします。以下のコマンドをColabのセルで実行してください。

!wget https://www.mathworks.com/mpm/glnxa64/mpm
!chmod +x mpm
!./mpm install --release=R2025a --products MATLAB Parallel_Computing_Toolbox

以下のようなログが表示されます
image 1.png

2.2. 認証用起動スクリプト

以下のようにディレクトリを作成し、その中に起動時に必要なスクリプトを作成します。

!mkdir -p /content/mw
%%writefile /content/mw/startup.m
disp('[startup] 認証完了 → 自動終了します');
pause(0.2);
quit

2.3. ライセンス認証

次に、インストールしたMATLABを使用するため、MathWorksアカウントでライセンス認証を行います。以下のコマンドを実行すると、対話形式の認証プロセスが開始されます。

!/usr/local/MATLAB/R2025a/bin/matlab -nodesktop -licmode onlinelicensing -sd /content/mw

実行後、画面の指示に従い、まずMathWorksアカウントのメールアドレスを入力します。
image 2.png

続いて、下図赤枠で示されるリンクからワンタイムパスワードを取得します。
image 3.png

リンクに飛ぶと以下のような画面に遷移します。ワンタイムパスワードをコピーしてください。
image 4.png

ワンタイムパスワードを入力欄に貼り付けることで認証が完了します。ライセンス認証に成功する以下のような画面が表示されます。処理が終わらない場合には手動で処理を終了してください。

image 5.png

3. 実行するMATLABコードの作成

次に実行するMATLABコードを作成します。ここでは、大規模な行列同士の積を計算させ、その処理時間をCPUとGPUのそれぞれで計測します。

以下のコードは、main.mという名前のMATLABスクリプトファイルを作成するものです。


 %%writefile main.m
 % 大規模行列積で CPU vs GPU の計算時間を比較(GPUなければCPUのみ)
 n = 4096;                    
 dtype = 'single';                
 
 % データ作成(CPUメモリ上)
 A_cpu = rand(n,n,dtype);
 B_cpu = rand(n,n,dtype);
 
 % --- CPUでの計算時間を計測 ---
 tic;
 C_cpu = A_cpu * B_cpu; %ok<NASGU>
 t_cpu = toc;
 
 % --- GPUが利用可能であれば、GPUでの計算時間を計測 ---
 if gpuDeviceCount > 0
     disp('GPUを検出しました。CPUとの性能比較を実行します。');
     g = gpuDevice; %ok<NASGU>
 
     % 計算時間のみを純粋に測るため、データはGPUメモリ上で直接生成します
     A_gpu = gpuArray.rand(n,n,dtype);
     B_gpu = gpuArray.rand(n,n,dtype);
 
     % ウォームアップ(初回実行時のオーバーヘッドを計測から除外)
     X = A_gpu * B_gpu; wait(gpuDevice); %ok<NASGU>
 
     % 本計測
     tic;
     C_gpu = A_gpu * B_gpu; %ok<NASGU>
     wait(gpuDevice); % GPUの処理が完了するのを待ちます
     t_gpu = toc;
 
     % 結果の表示
     fprintf('[CPU] %.3f s\n', t_cpu);
     fprintf('[GPU] %.3f s\n', t_gpu);
     fprintf('高速化倍率 (CPU/GPU): %.2fx\n', t_cpu / t_gpu);
 else
     disp('GPUが見つかりませんでした。CPUの計算時間のみを計測しました。');
     fprintf('[CPU] %.3f s\n', t_cpu);
 end

4. スクリプトの実行と結果の確認

最後に、作成した main.m を実行します。-batch オプションを使用することで、MATLABのGUIを立ち上げることなく、コマンドラインでスクリプトを実行し、結果を標準出力に表示させることができます。

%作成した'main.m'をバッチモードで実行します
!/usr/local/MATLAB/R2025a/bin/matlab -batch "run('main.m')" -licmode onlinelicensing

実行結果の一例を以下に示します
image 6.png

5. まとめ

本記事では、Colab上にMATLABの実行環境を構築し、GPUを利用して計算を高速化する具体的な手順を解説しました。こうすることで、ローカルマシンのリソースに制約されることなく、大規模な計算タスクを効率的に実行するための選択肢となります。

6. 全体コード

!wget https://www.mathworks.com/mpm/glnxa64/mpm
!chmod +x mpm
!./mpm install --release=R2025a --products MATLAB Parallel_Computing_Toolbox

!mkdir -p /content/mw

 Commented out IPython magic to ensure Python compatibility.
 %%writefile /content/mw/startup.m
 disp('[startup] 認証完了 → 自動終了します');
 pause(0.2);
 quit

!/usr/local/MATLAB/R2025a/bin/matlab -nodesktop -licmode onlinelicensing -sd /content/mw

 Commented out IPython magic to ensure Python compatibility.
 %%writefile main.m
 % 大規模行列積で CPU vs GPU の計算時間を比較(GPUなければCPUのみ)
 n = 4096;                   % きつければ 2048 に下げて
 dtype = 'single';           % GPU向けに単精度
 
 % データ作成(CPU)
 A_cpu = rand(n,n,dtype);
 B_cpu = rand(n,n,dtype);
 
 % --- CPU計測 ---
 tic;
 C_cpu = A_cpu * B_cpu; %ok<NASGU>
 t_cpu = toc;
 
 % --- GPU計測(あれば) ---
 if gpuDeviceCount > 0
     disp('GPU検出: 比較を実行します');
     g = gpuDevice; %ok<NASGU>
     % 計算だけの時間を測るため、データはGPU上で生成(転送時間を排除)
     A_gpu = gpuArray.rand(n,n,dtype);
     B_gpu = gpuArray.rand(n,n,dtype);
     % ウォームアップ(初回オーバーヘッドを除外)
     X = A_gpu * B_gpu; wait(gpuDevice); %ok<NASGU>
     % 本計測
     tic;
     C_gpu = A_gpu * B_gpu; %ok<NASGU>
     wait(gpuDevice);
     t_gpu = toc;
 
     fprintf('[CPU]  %.3f s\n', t_cpu);
     fprintf('[GPU]  %.3f s\n', t_gpu);
     fprintf('Speedup (CPU/GPU): %.2fx\n', t_cpu / t_gpu);
 else
     disp('GPUが見つからないためCPUのみ計測しました。');
     fprintf('[CPU]  %.3f s\n', t_cpu);
 end

!/usr/local/MATLAB/R2025a/bin/matlab -batch "run('main.m')" -licmode onlinelicensing
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?