主要内容

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

FileStore

R2026b

MATLAB 客户端和工作单元共享的文件存储

自 R2022a 起

描述

FileStore 是存储特定作业所拥有的文件的对象。对象的每个条目由一个文件及其对应的键组成。当所属作业被删除时,FileStore 对象也会被删除。使用 FileStore 存储来自 MATLAB® 工作单元的文件,这些文件可以在作业执行期间由 MATLAB 客户端检索(即使作业仍在运行)。

  • 任何 MATLAB 进程客户端或工作单元都可以随时将条目写入 FileStore。任何 MATLAB 进程客户端或工作单元都可以随时从 FileStore 读取此条目。然而,不同进程执行操作的顺序并不能保证。

  • 当集群没有共享文件系统时,可以使用 FileStore 返回文件,或者运行不关心任何共享文件系统位置的代码。

  • FileStore 存储在作业存储位置,而非系统内存中,因此可用于存储大型结果。

创建对象

当您创建以下内容时,会自动创建 FileStore 对象:

  • 集群上的作业,它是一个 parallel.Job 对象。要创建作业,请使用 batch、createJob 或 createCommunicatingJob 函数。

  • 本地计算机上的进程工作单元并行池,它是一个 ProcessPool 对象。要创建进程池,请使用 parpool 函数。

  • 本地计算机上线程式工作单元的并行池,它是一个 ThreadPool 对象。要创建线程池,请使用 parpool 函数。 (自 R2023b 起)

  • 计算机集群上的并行工作单元池,它是一个 ClusterPool 对象。要创建集群池,请使用 parpool 函数。

您可以使用 getCurrentFileStore 函数访问工作单元上的 FileStore 对象。然后,您可以使用与作业或并行池关联的 FileStore 属性在客户端上检索 FileStore 对象。有关示例,请参阅运行批处理作业并从工作单元中检索文件。

属性

全部展开

添加或替换条目时执行的回调,指定为函数句柄。当添加或替换条目时,函数句柄必须接受两个输入参量,分别代表 FileStore 对象及其键。

当条目被删除时执行的回调,指定为函数句柄。当条目被删除时,函数句柄必须接受两个输入参量,它们代表 FileStore 对象及其键。

对象函数

isKey确定 ValueStore 或 FileStore 对象是否包含键
keys返回 ValueStore 或 FileStore 对象的所有键
copyFileToStore将文件从本地文件系统复制到 FileStore 对象
copyFileFromStore将文件从 FileStore 对象复制到本地文件系统
remove从 ValueStore 或 FileStore 对象中删除条目

示例

全部折叠

对工作单元运行仿真并在客户端上检索作业的文件存储。文件存储是一个带有关键文件条目的 FileStore 对象。

下面的仿真找到随机矩阵的平均值和标准差,并将结果存储在 FileStore 对象中。

type workerStatsCode
function workerStatsCode(models)
% Get the FileStore of the current job
store = getCurrentFileStore;
for i = 1:numel(models)
    % Compute the average and standard deviation of random matrices
    A = rand(models(i));
    M = mean(A);
    S = std(A);
    % Save simulation results in temporary files
    sourceTempFile = strcat(tempname("C:\myTempFolder"),".mat");
    save(sourceTempFile,"M","S");
    % Copy files to FileStore object as key-file pairs
    key = strcat("result_",num2str(i));
    copyFileToStore(store,sourceTempFile,key);
end
end

当文件被复制到 FileStore 对象时,将执行以下回调函数。

type fileNewEntry
function fileNewEntry(store,key)
   destination = strcat(key,".mat");
   fprintf("Result %s added. Copying to local file system: %s\n",key,destination);
   copyFileFromStore(store,key,destination);
end

使用默认集群配置文件在工作单元上运行批处理作业。

models = [4,8,32,20];
c = parcluster;
job = batch(c,@workerStatsCode,0,{models});

在作业仍在运行时检索客户端上的 FileStore 对象。显示作业的进度。

store = job.FileStore;
store.KeyUpdatedFcn = @fileNewEntry;
wait(job);
Result result_1 added. Copying to local file system: result_1.mat
Result result_2 added. Copying to local file system: result_2.mat
Result result_3 added. Copying to local file system: result_3.mat
Result result_4 added. Copying to local file system: result_4.mat

显示文件 "result_3.mat" 中存储的变量的所有信息。

whos -file 'result_3.mat'
  Name      Size            Bytes  Class     Attributes

  M         1x32              256  double              
  S         1x32              256  double              

在进程工作单元的并行池上运行仿真并检索客户端上的文件存储。

下面的仿真找到随机矩阵的平均值和标准差,并将结果存储在 FileStore 对象中。

type workerStatsCode
function workerStatsCode(models)
% Get the FileStore of the current job
store = getCurrentFileStore;
for i = 1:numel(models)
    % Compute the average and standard deviation of random matrices
    A = rand(models(i));
    M = mean(A);
    S = std(A);
    % Save simulation results in temporary files
    sourceTempFile = strcat(tempname("C:\myTempFolder"),".mat");
    save(sourceTempFile,"M","S");
    % Copy files to FileStore object as key-file pairs
    key = strcat("result_",num2str(i));
    copyFileToStore(store,sourceTempFile,key);
end
end

当文件被复制到 FileStore 对象时,将执行以下回调函数。

type fileNewEntry
function fileNewEntry(store,key)
   destination = strcat(key,".mat");
   fprintf("Result %s added. Copying to local file system: %s\n",key,destination);
   copyFileFromStore(store,key,destination);
end

启动一个进程工作单元的并行池。

pool = parpool('Processes');
Starting parallel pool (parpool) using the 'Processes' profile ...
Connected to parallel pool with 6 workers.

获取此池的 FileStore 并分配在添加条目时执行的回调函数。

store = pool.FileStore;
store.KeyUpdatedFcn = @fileNewEntry;

在池上运行仿真。

models = [4,8,32,20];
future = parfeval(@workerStatsCode,0,models);
wait(future);
Result result_1 added. Copying to local file system: result_1.mat
Result result_2 added. Copying to local file system: result_2.mat
Result result_3 added. Copying to local file system: result_3.mat
Result result_4 added. Copying to local file system: result_4.mat

显示存储在本地文件 result_3.mat 中的变量。

whos -file 'result_3.mat'
  Name      Size            Bytes  Class     Attributes

  M         1x32              256  double              
  S         1x32              256  double              

运行一项独立任务的作业。然后,在客户端上检索该作业的数据和文件存储。

下面的仿真找到向量的排列和组合,并将结果存储在 ValueStore 和 FileStore 对象中。

type taskFunction
function taskFunction(dataset,keyname)
% Get the ValueStore and FileStore of the current job
valueStore = getCurrentValueStore;
fileStore = getCurrentFileStore;
% Run the simulation to find permutation and combination
[result,logFile] = runSimulation(dataset);
% Store results in ValueStore to release system memory
valueStore(keyname) = result;
% Copy file to FileStore to retrieve the file from non-shared file system
copyFileToStore(fileStore,logFile,keyname);
end

function [result,logFile] = runSimulation(dataset)
    permutations = perms(dataset{1});
    combinations = nchoosek(dataset{1},dataset{2});
    result.N_perm = length(permutations);
    result.N_comb = length(combinations);
    logFile = strcat(tempname("C:\myLogFolder"),".mat");
    save(logFile,"permutations","combinations")
end

使用默认集群配置文件创建作业。

c = parcluster;
job = createJob(c);

为作业创建独立的任务。每个任务使用给定的输入运行仿真。

set_1 = {[12,34,54],2};
set_2 = {[45,33],1};
set_3 = {[12,12,12,13,14],3};
tasks = createTask(job,@taskFunction,0,{{set_1,"sim_1"},{set_2,"sim_2"},{set_3,"sim_3"}});

运行该作业并等待其完成。

submit(job);
wait(job);

检索作业的数据和文件存储。

valueStore = job.ValueStore;
fileStore = job.FileStore;

显示存储在 ValueStore 对象中的第三个任务的结果。

result_3 = valueStore("sim_3")
result_3 = struct with fields:
    N_perm: 120
    N_comb: 10

将文件从相应键 "sim_1" 和 "sim_2" 指定的文件存储复制到本地文件 "analysis_1.mat" 和 "analysis_2.mat"。

copyFileFromStore(fileStore,["sim_1" "sim_2"],["analysis_1.mat" "analysis_2.mat"]);

显示存储在本地文件中的变量的所有信息。

whos -file 'analysis_1.mat'
  Name              Size            Bytes  Class     Attributes

  combinations      3x2                48  double              
  permutations      6x3               144  double              
whos -file 'analysis_2.mat'
  Name              Size            Bytes  Class     Attributes

  combinations      2x1                16  double              
  permutations      2x2                32  double              

限制

  • FileStore 密钥长度不得超过 249 个字符。

  • 当使用 parallel.cluster.Generic 集群并将 'HasSharedFileSystem' 设置为 false 时,作业运行时对 FileStore 所做修改的可见性取决于您的具体实现。如果 MATLAB 客户端和工作单元 JobStorageLocation 之间没有额外的同步,那么只有在作业完成后才能看到更改。

提示

  • 您分配给 KeyUpdatedFcn 和 KeyRemovedFcn 属性的回调函数仅适用于当前的 MATLAB 客户端会话。将批处理作业提交到远程计算集群后,在作业处理期间,您可以关闭客户端会话。当您稍后启动一个新的客户端会话时,MATLAB 不会保留该作业的FileStore的原始回调函数。若要在新的客户端会话中继续执行回调函数,请从 parallel.Job 对象中获取 FileStore,并为 KeyUpdatedFcn 和 KeyRemovedFcn 属性重新分配回调函数。

版本历史记录

在 R2022a 中推出

全部展开