Share via


tile_static Schlüsselwort

Die tile_static Schlüsselwort wird verwendet, um eine Variable deklarieren, die von allen Threads in einer Kachel Threads zugegriffen werden kann.Die Lebensdauer der Variablen beginnt bei der Ausführung erreicht den Punkt der Deklaration und endet, wenn die Kernfunktion gibt.Weitere Informationen zur Verwendung von Kacheln finden Sie unter Verwenden von Kacheln.

Die tile_static Schlüsselwort weist die folgenden Einschränkungen:

  • Es kann nur auf Variablen, die in einer Funktion befinden, verwendet die restrict(amp) Modifizierer.

  • Es kann nicht auf Variablen verwendet werden, die Zeiger oder Verweis Typen sind.

  • A tile_static Variable kann keinen Initialisierer.Standardkonstruktoren und Destruktoren werden nicht automatisch aufgerufen.

  • Der Wert einer nicht initialisierten tile_static Variable ist nicht definiert.

  • Wenn ein tile_static Variable deklariert ist, in einem Call-Diagramm, das durch einen Aufruf nicht gekachelt verankert ist parallel_for_each, wird eine Warnung generiert, und das Verhalten der Variable ist nicht definiert.

Beispiel

Das folgende Beispiel zeigt wie ein tile_static Variable kann zum Sammeln von Daten über mehrere Threads in einer Fläche.

// Sample data:
int sampledata[] = {
    2, 2, 9, 7, 1, 4,
    4, 4, 8, 8, 3, 4,
    1, 5, 1, 2, 5, 2,
    6, 8, 3, 2, 7, 2};

// The tiles:
// 2 2    9 7    1 4
// 4 4    8 8    3 4
//
// 1 5    1 2    5 2
// 6 8    3 2    7 2

// Averages:
int averagedata[] = { 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
};

array_view<int, 2> sample(4, 6, sampledata);
array_view<int, 2> average(4, 6, averagedata);

parallel_for_each(
    // Create threads for sample.extent and divide the extent into 2 x 2 tiles.
    sample.extent.tile<2,2>(),
    [=](tiled_index<2,2> idx) restrict(amp)
    {
        // Create a 2 x 2 array to hold the values in this tile.
        tile_static int nums[2][2];
        // Copy the values for the tile into the 2 x 2 array.
        nums[idx.local[1]][idx.local[0]] = sample[idx.global];
        // When all the threads have executed and the 2 x 2 array is complete, find the average.
        idx.barrier.wait();
        int sum = nums[0][0] + nums[0][1] + nums[1][0] + nums[1][1];
        // Copy the average into the array_view.
        average[idx.global] = sum / 4;
      }
);

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 6; j++) {
        std::cout << average(i,j) << " ";
    }
    std::cout << "\n";
}

// Output:
// 3 3 8 8 3 3
// 3 3 8 8 3 3
// 5 5 2 2 4 4
// 5 5 2 2 4 4
// Sample data.
int sampledata[] = {
    2, 2, 9, 7, 1, 4,
    4, 4, 8, 8, 3, 4,
    1, 5, 1, 2, 5, 2,
    6, 8, 3, 2, 7, 2};

// The tiles are:
// 2 2    9 7    1 4
// 4 4    8 8    3 4
//
// 1 5    1 2    5 2
// 6 8    3 2    7 2

// Averages.
int averagedata[] = { 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 
};

array_view<int, 2> sample(4, 6, sampledata);
array_view<int, 2> average(4, 6, averagedata);

parallel_for_each(
    // Create threads for sample.grid and divide the grid into 2 x 2 tiles.
    sample.extent.tile<2,2>(),
    [=](tiled_index<2,2> idx) restrict(amp)
    {
        // Create a 2 x 2 array to hold the values in this tile.
        tile_static int nums[2][2];
        // Copy the values for the tile into the 2 x 2 array.
        nums[idx.local[1]][idx.local[0]] = sample[idx.global];
        // When all the threads have executed and the 2 x 2 array is complete, find the average.
        idx.barrier.wait();
        int sum = nums[0][0] + nums[0][1] + nums[1][0] + nums[1][1];
        // Copy the average into the array_view.
        average[idx.global] = sum / 4;
      }
);

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 6; j++) {
        std::cout << average(i,j) << " ";
    }
    std::cout << "\n";
}

// Output.
// 3 3 8 8 3 3
// 3 3 8 8 3 3
// 5 5 2 2 4 4
// 5 5 2 2 4 4

Siehe auch

Aufgaben

Exemplarische Vorgehensweise: Matrixmultiplikation

Referenz

Microsoft-spezifische Modifizierer

parallel_for_each-Funktion (C++ AMP)

Konzepte

Übersicht über C++ AMP