您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .NET 的 Azure Batch 库Azure Batch libraries for .NET

Azure Batch 是一项平台服务,适用于在云中有效运行大规模并行和高性能计算 (HPC) 应用程序。Azure Batch is a platform service for running large-scale parallel and high-performance computing (HPC) applications efficiently in the cloud. Azure Batch 可以计划要在托管的虚拟机集合上运行的计算密集型工作,并且可以缩放计算资源,使之符合作业的需求。Azure Batch schedules compute-intensive work to run on a managed collection of virtual machines, and can automatically scale compute resources to meet the needs of your jobs.

使用 Azure Batch 时,可以轻松定义用于大规模并行执行应用程序的 Azure 计算资源。With Azure Batch, you can easily define Azure compute resources to execute your applications in parallel, and at scale. 不需要手动创建、配置和管理 HPC 群集、各个虚拟机、虚拟网络或复杂的作业和任务计划基础结构。There's no need to manually create, configure, and manage an HPC cluster, individual virtual machines, virtual networks, or a complex job and task scheduling infrastructure. Azure Batch 自动执行这些任务,或者为用户简化这些任务。Azure Batch automates or simplifies these tasks for you.

详细了解如何使用 Batch 运行固有并行的工作负荷Read more about how to run intrinsically parallel workloads with Batch. 此外,还可以详细如何使用用于 .NET 的 Batch 客户端库开始构建解决方案You can also learn how to get started building solutions with the Batch client library for .NET. 了解如何使用用于 .NET 的 Batch 管理库来管理 Batch 帐户和配额Discover how to manage Batch accounts and quotas with the Batch Management library for .NET.

客户端库Client library

在 Batch 中使用客户端库运行并行工作负荷。Use the client library to run parallel workloads with Batch.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Azure.Batch

.NET Core CLI.NET Core CLI

dotnet add package Azure.Batch

示例Example

以下示例使用客户端 SDK 创建在 Azure Batch 中运行的作业。The following example uses the client SDK to create a job to run in Azure Batch.

/*
using Microsoft.Azure.Batch.Auth;
using Microsoft.Azure.Batch;
*/
BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials(batchUrl, accountName, accountKey);
using (BatchClient batchClient = await BatchClient.OpenAsync(credentials))
{
    //set up pool specification and information along with resource files here
    JobManagerTask jobManagerTask = new JobManagerTask()
    {
        ResourceFiles = jobManagerResourceFiles,
        CommandLine = Constants.JobManagerExecutable,

        //Determines if the job should terminate when the job manager process exits.
        KillJobOnCompletion = true,
        Id = jobManagerTaskId
    };

    string jobId = Environment.GetEnvironmentVariable("USERNAME") + DateTime.UtcNow.ToString("yyyyMMdd-HHmmss");

    CloudJob unboundJob = batchClient.JobOperations.CreateJob(jobId, poolInformation);
    unboundJob.JobManagerTask = jobManagerTask;

    // now interact with the job ...
}

管理库Management library

使用管理库可通过编程方式管理 Batch 帐户、配额和应用程序包。Use the management library to programmatically manage Batch accounts, quotas, and application packages.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 包管理器Visual Studio Package Manager

Install-Package Microsoft.Azure.Management.Batch

.NET Core CLI.NET Core CLI

dotnet add package Microsoft.Azure.Management.Batch

示例Example

以下示例检索订阅的配额、创建帐户,并重新生成主帐户密钥。The following example retrieves the quota for the subscription, creates an account, and regenerates the primary account key.

/*
using Microsoft.Azure.Management.Batch;
using Microsoft.Azure.Management.Batch.Models;
using Microsoft.Rest;
*/
using (BatchManagementClient batchManagementClient = new BatchManagementClient(new TokenCredentials(accessToken)))
{
    batchManagementClient.SubscriptionId = subscriptionId;

    // Get the account quota for the subscription
    BatchLocationQuota quotaResponse = await batchManagementClient.Location.GetQuotasAsync(location);
    Console.WriteLine("Your subscription can create {0} account(s) in the {1} region.", quotaResponse.AccountQuota, location);

    // Create account
    await batchManagementClient.BatchAccount.CreateAsync(ResourceGroupName, accountName, 
        new BatchAccountCreateParameters() { Location = location });

    // Regenerate primary account key
    BatchAccountKeys newKeys = await batchManagementClient.BatchAccount.RegenerateKeyAsync(
        ResourceGroupName, account.Name, AccountKeyType.Primary);
}

示例Samples

详细了解可在应用中使用的示例 .NET 代码Explore more sample .NET code you can use in your apps.