主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

Java 应用程序中指定 Parallel Computing Toolbox 配置文件

此示例说明如何使用 MATLAB® Runtime 用户数据接口指定 Java® 应用程序中 Parallel Computing Toolbox™ 集群的配置文件。

有关详细信息,请参阅使用 MATLAB Runtime 用户数据接口

步骤 1:编写 Parallel Computing Toolbox 代码

  1. 在 MATLAB 中创建 sample_pct.m

    此示例代码使用 Parallel Computing Toolbox 默认配置文件中定义的集群。

    function speedup = sample_pct (n)
    warning off all;
    tic
    if(ischar(n))
        n=str2double(n);
    end
    for ii = 1:n
       (cov(sin(magic(n)+rand(n,n))));
    end
    time1 =toc;
    parpool;
    tic
    parfor ii = 1:n
       (cov(sin(magic(n)+rand(n,n))));
    end
    time2 =toc;
    disp(['Normal loop time: ' num2str(time1) ...
        ', parallel loop time: ' num2str(time2) ]);
    disp(['parallel speedup: ' num2str(1/(time2/time1)) ...
        ' times faster than normal']);
    delete(gcp);
    disp('done');
    speedup = (time1/time2);
    
  2. 使用输入 400 运行该函数。

    a = sample_pct(400)

  3. 以下是输出的示例,假设默认配置文件设置为 local

    Starting parallel pool (parpool) using the 'local' profile ...
    Connected to the parallel pool (number of workers: 6).
    Normal loop time: 2.5651, parallel loop time: 1.6371
    parallel speedup: 1.5668 times faster than normal
    Parallel pool using the 'local' profile is shutting down.
    done
    
    ans =
    
        1.5668

步骤 2:设置 Parallel Computing Toolbox 配置文件

要使用由 MATLAB Compiler SDK™ 构建的 Java 包访问 MATLAB Runtime 用户数据接口,您必须直接从 MATLAB 设置 mcruserdata。没有 Java API 来访问 mcruserdata,因为有使用 MATLAB Compiler SDK 构建的 C 和 C++ 应用程序。

要从 MATLAB 设置 mcruserdata,请创建一个 init 函数。这个单独的 MATLAB 函数使用 setmcruserdata 来设置一次 Parallel Computing Toolbox 文件。然后调用其他函数来利用 Parallel Computing Toolbox。

创建以下 init_sample_pct 函数:

function init_sample_pct
% Set the Parallel Computing Toolbox Profile:
if(isdeployed)
    % Let the USER select the cluster profile.
    [profile, profpath] = uigetfile('*.mlsettings'); 
    setmcruserdata('ParallelProfile', fullfile(profpath, profile));
end

要将现有配置文件导出到 .mlsettings 文件,请使用 parallel.exportProfile (Parallel Computing Toolbox) 函数。例如,

parallel.exportProfile('local','mylocalsettings');

提示

如果您需要在应用程序中更改配置文件,请使用 parallel.importProfile (Parallel Computing Toolbox)parallel.defaultClusterProfile (Parallel Computing Toolbox)。有关详细信息,请参阅发现集群并使用集群配置文件 (Parallel Computing Toolbox)

步骤 3:将您的代码编译成 Java

使用 Java 包编译器compiler.build.javaPackage 构建 Java 包。

将以下信息用于您的工程:

包名称parallelComponent
类名PctClass
要编译的文件sample_pct.minit_pct_sample.m

例如,如果您使用的是 compiler.build.javaPackage,请输入:

buildResults = compiler.build.javaPackage( ...
{'sample_pct.m','init_sample_pct.m'}, ...
'PackageName','parallelComponent','ClassName','PctClass');

有关详细信息,请参阅生成 Java 包并编译 Java 应用程序中的说明。

注意

如果您正在使用 Parallel Computing Toolbox 的 GPU 功能,则必须手动添加 PTX 和 CU 文件。

  • 如果您使用的是编译器,请点击自定义需求部分中的添加文件

  • 如果您正在使用 compiler.build 函数,请使用 AdditionalFiles 选项。

  • 如果您使用 mcc 命令,请使用 -a 选项。

步骤 4:编写 Java 应用程序

为访问 MATLAB 函数的 Java 应用程序编写源代码。将此代码保存为包含生成的 JavaParallelClass.java 包的文件夹中的 parallelComponent.jar

下面提供了此示例的示例应用程序。

import com.mathworks.toolbox.javabuilder.*;
import parallelComponent.*;
 
public class JavaParallelClass 
{
	public static void main(String[] args) 
	{
		 MWArray A = null;
		 PctClass C = null;
		 Object[] B = null;
		 try
		 {
			 C = new PctClass();
			 /* Set up the runtime with Parallel Data */
			 C.init_sample_pct();
			 A = new MWNumericArray(400);
			 B = C.sample_pct(1, A);
			 System.out.println("The speed up was: " + B[0]);
		 }
		 catch (Exception e)
		 {
			 System.out.println("The error is " + e.toString());
		 }
		 finally
		 {
			 MWArray.disposeArray(A);
			 C.dispose();
		 }
	}
}

编译并运行应用程序

使用 IDE 或命令提示符编译 Java 应用程序。

  • 在 Windows® 上,执行以下命令:

    javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\parallelComponent.jar JavaParallelClass.java
  • 在 UNIX® 上,执行以下命令:

    javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./parallelComponent.jar JavaParallelClass.java

matlabroot 替换为 MATLAB 或 MATLAB Runtime 安装文件夹的路径。例如,在 Windows 上,默认路径是 C:\Program Files\MATLAB\R2025a

运行 JavaParallelClass 应用程序。

  • 在 Windows 上,执行以下命令:

    java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\parallelComponent.jar JavaParallelClass
  • 在 UNIX 上,执行以下命令:

    java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./parallelComponent.jar JavaParallelClass
    

注意

如果您在 Mac 64 位平台上运行该应用程序,则必须在 -d64 命令中添加 java 标志。

JavaParallelClass 应用程序提示您选择要使用的集群配置文件。选择 .mlsettings 文件后,应用程序将显示类似以下内容的输出:

Starting parallel pool (parpool) using the 'local_mcruserdata' profile ...
Connected to the parallel pool (number of workers: 6).
Normal loop time: 2.428, parallel loop time: 1.6515
parallel speedup: 1.4701 times faster than normal
Parallel pool using the 'local_mcruserdata' profile is shutting down.
done
The speed up was: 1.4701

另请参阅

|

主题