Export (0) Print
Expand All

Filtering and Sorting Lists of Objects

When retrieving jobs, tasks, and nodes, you can filter and sort the results. The following C# and C++ examples show how to filter the nodes that you want to retrieve.


#include <windows.h>
#include <stdio.h>

// The Microsoft.Hpc.Scheduler.tlb and Microsoft.Hpc.Scheduler.Properties.tlb type
// libraries are included in the Microsoft HPC Pack 2008 SDK. The type libraries are
// located in the "Microsoft HPC Pack 2008 SDK\Lib\i386" or \amd64 folder. Include the rename 
// attributes to avoid name collisions.
#import <Microsoft.Hpc.Scheduler.tlb> named_guids no_namespace raw_interfaces_only \
    rename("SetEnvironmentVariable","SetHpcEnvironmentVariable") \
    rename("AddJob", "AddHpcJob")
#import <Microsoft.Hpc.Scheduler.Properties.tlb> named_guids no_namespace raw_interfaces_only 

void EnumerateNodes(IScheduler* pScheduler);

void main(void)
{
    HRESULT hr = S_OK;
    IScheduler* pScheduler = NULL;

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    // Get an instance of the Scheduler object. 
    hr = CoCreateInstance( __uuidof(Scheduler), // CLSID_Scheduler, 
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           __uuidof(IScheduler), // IID_IScheduler, 
                           reinterpret_cast<void **> (&pScheduler) );

    if (FAILED(hr))
    {
        wprintf(L"CoCreateInstance(IScheduler) failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pScheduler->Connect(_bstr_t(L"localhost"));
    if (FAILED(hr))
    {
        wprintf(L"pScheduler->Connect failed with 0x%x.\n", hr);
        goto cleanup;
    }

    wprintf(L"Connected to localhost\n\n");

    EnumerateNodes(pScheduler);

cleanup:

    // Before exiting, release your instance of IScheduler.
    if (pScheduler)
        pScheduler->Release();

    CoUninitialize();
}


void EnumerateNodes(IScheduler* pScheduler)
{
    HRESULT hr = S_OK;
    ISchedulerCollection* pNodes = NULL;
    ISchedulerNode* pNode = NULL;
    IFilterCollection* pFilters = NULL;
    ISortCollection* pSortOn = NULL;
    VARIANT var;
    long count = 0;
    long numberOfCores = 0;
    long cpuSpeed = 0;
    __int64 memorySize = 0;
    BSTR bstr = NULL;


    // Specify the filter criteria.
    hr = pScheduler->CreateFilterCollection(&pFilters);
    if (FAILED(hr))            
    {
        wprintf(L"pScheduler->CreateFilterCollection failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pFilters->Add(FilterOperator_GreaterThanOrEqual, PropId_Node_NumCores, _variant_t(1));
    if (FAILED(hr))            
    {
        wprintf(L"pFilters->Add(NumCores) failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pFilters->Add(FilterOperator_HasBitSet, PropId_Node_State, _variant_t(NodeState_Online));
    if (FAILED(hr))            
    {
        wprintf(L"pFilters->Add(State) failed with 0x%x.\n", hr);
        goto cleanup;
    }

    // Specify the sort criteria.
    hr = pScheduler->CreateSortCollection(&pSortOn);
    if (FAILED(hr))            
    {
        wprintf(L"pScheduler->CreateSortCollection failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pSortOn->Add(SortOrder_Ascending, PropId_Node_NumCores);
    if (FAILED(hr))            
    {
        wprintf(L"pSortOn->Add(NumCores) failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pSortOn->Add(SortOrder_Ascending, PropId_Node_CpuSpeed);
    if (FAILED(hr))            
    {
        wprintf(L"pSortOn->Add(CpuSpeed) failed with 0x%x.\n", hr);
        goto cleanup;
    }

    // Get the list of nodes that match the filter criteria
    // and return them in the specified sort order.
    hr = pScheduler->GetNodeList(pFilters, pSortOn, &pNodes);
    if (FAILED(hr))            
    {
        wprintf(L"pScheduler->GetJobIdList failed with 0x%x.\n", hr);
        goto cleanup;
    }

    hr = pNodes->get_Count(&count);
    if (FAILED(hr))            
    {
           wprintf(L"pNodes->get_Count failed with 0x%x.\n", hr);
        goto cleanup;
    }

    VariantInit(&var);

    if (count > 0)
    {
        wprintf(L"The following nodes match the filter criteria.\n");
        wprintf(L"Name\t\tCores\tCPU Speed\tMemory\n");

        for (long i = 0; i < count; i++)
        {
            hr = pNodes->get_Item(i, &var);
            if (FAILED(hr))
            {
                wprintf(L"pNodes->get_Item failed with 0x%x.\n", hr);
                goto cleanup;
            }

            var.pdispVal->QueryInterface(IID_ISchedulerNode, reinterpret_cast<void **>(&pNode));
            VariantClear(&var);

            hr = pNode->get_Name(&bstr);
            if (FAILED(hr))
            {
                wprintf(L"pNode->get_Name failed with 0x%x.\n", hr);
                goto cleanup;
            }

            hr = pNode->get_NumberOfCores(&numberOfCores);
            if (FAILED(hr))
            {
                wprintf(L"pNode->get_NumberOfCores failed with 0x%x.\n", hr);
                goto cleanup;
            }

            hr = pNode->get_CpuSpeed(&cpuSpeed);
            if (FAILED(hr))
            {
                wprintf(L"pNode->get_CpuSpeed failed with 0x%x.\n", hr);
                goto cleanup;
            }

            hr = pNode->get_MemorySize(&memorySize);
            if (FAILED(hr))
            {
                wprintf(L"pNode->get_MemorySize failed with 0x%x.\n", hr);
                goto cleanup;
            }

            wprintf(L"%s\t%ld\t%.2f GHz\t%.2f GB\n", 
                    bstr, numberOfCores, 
                    cpuSpeed / 1000.0, memorySize / 1000.0);

            pNode->Release();
            pNode = NULL;
        }
    }
    else
    {
        wprintf(L"No nodes match the filter criteria.");
    }

cleanup:

    if (pNodes)
        pNodes->Release();;

    if (pNode)
        pNode->Release();

    if (pFilters)
        pFilters->Release();

    if (pSortOn)
        pSortOn->Release();

    if (bstr)
        SysFreeString(bstr);
}


Related topics

Using HPC

 

 

Show:
© 2014 Microsoft