简体   繁体   English

在类中创建静态方法和字段

[英]Make static methods and fields in class

I'm rewriting algorithm to GPU with Cudafy. 我正在使用Cudafy将算法重写为GPU。 I need to call Execute() from a static method. 我需要静态方法调用Execute() It's necessary to calculate on GPU. 有必要在GPU上进行计算。 How could I do this? 我该怎么办? What fields or anything should I copy into static? 我应将哪些字段或任何内容复制到静态内容?

Object of the class is called from non-static method and it couldn't be changed. 该类的对象是从非静态方法调用的,无法更改。 It creates an objects, makes Execute (ideally), and gets triangles as result. 它创建一个对象,使(理想情况下)执行,并获得三角形作为结果。

The class code is: 类代码为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

using DICOMViewer.Helper;
using DICOMViewer.Volume;
using DICOMViewer.ImageFlow;

using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;

namespace DICOMViewer.GPU
{
    class MarchingCubesOnGPU
    {
        private static List<Triangle> theTriangles;
        // CPU fields
        private Point3D[] points = new Point3D[8];
        private int[] values = new int[8];
        private double theIsoValue;
        private List<Triangle> triangles;

        // GPU fields
        private static PointGPU[] pointsGpu = new PointGPU[8];
        static int[] valsGpu = new int[8];
        private static double[] isolevelGpu = new double[1];
        private static TriangleGPU[] trianglesGpu;

        public static int[] EdgeTableStatic = new int[256]
        {
           // here are the values for edgeTable from the algorithm link
        };

        public static int[,] TriTableStatic = new int[256, 16]
        {
            // here are the values for triTable from the algorithm link
        };

        public MarchingCubesOnGPU(GridCell grid, double isolevel, List<Triangle> theTriangleList)
        {
            points = grid.p;
            values = grid.val;
            this.theIsoValue = isolevel;
            triangles = theTriangleList;
            theTriangles = new List<Triangle>();

            ConvertToStandardTypes();
            Execute(); // ???
        }

        public List<Triangle> getTriangles()
        {
            return theTriangles;
        }

        // Convert fields for GPU
        public void ConvertToStandardTypes()
        {
            for (int i = 0; i < points.Length; i++)
            {
                pointsGpu[i].x = points[i].X;
                pointsGpu[i].y = points[i].Y;
                pointsGpu[i].z = points[i].Z;
            }

            valsGpu = values;
            isolevelGpu[0] = theIsoValue;

            for (int i = 0; i < triangles.Count; i++)
            {
                trianglesGpu[i].p[i].x = triangles[i].p[i].X;
                trianglesGpu[i].p[i].y = triangles[i].p[i].Y;
                trianglesGpu[i].p[i].z = triangles[i].p[i].Z;

            }
        }

        public static void Execute()
        {
            CudafyModule km = CudafyTranslator.Cudafy();
            GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
            gpu.LoadModule(km);

            TriangleGPU[] tris;

            PointGPU[] dev_points = gpu.Allocate(pointsGpu);
            int[] dev_values = gpu.Allocate(valsGpu);
            double[] dev_iso = gpu.Allocate<double>();
            TriangleGPU[] dev_triangles = gpu.Allocate(trianglesGpu);
            int[] dev_edgeTable = gpu.Allocate<int>();
            int[,] dev_triangleTable = gpu.Allocate(TriTableStatic);

            gpu.CopyToDevice(pointsGpu, dev_points);
            gpu.CopyToDevice(valsGpu, dev_values);
            gpu.CopyToDevice(isolevelGpu, dev_iso);
            gpu.CopyToDevice(trianglesGpu, dev_triangles);
            gpu.CopyToDevice(EdgeTableStatic, dev_edgeTable);
            gpu.CopyToDevice(TriTableStatic, dev_triangleTable);

            gpu.Launch().PolygoniseOnGpu(dev_iso, dev_edgeTable, dev_triangleTable, dev_points, dev_values, dev_triangles);

            for (int k = 0; k < dev_triangles.Length; k++)
            {
                gpu.CopyFromDevice(dev_triangles, out trianglesGpu[k]);
            }

            for (int i = 0; i < trianglesGpu.Length; i++)
            {
                theTriangles[i].p[i].X = trianglesGpu[i].p[i].x;
                theTriangles[i].p[i].Y = trianglesGpu[i].p[i].y;
                theTriangles[i].p[i].Z = trianglesGpu[i].p[i].z;
            }

            gpu.FreeAll();
        }

        [Cudafy]
        public void PolygoniseOnGpu(double[] iso, int[] edgeT, int[,] triT, PointGPU[] p, int[] v, TriangleGPU[] tri)
        {
            int cubeindex = 0;
            for (int i = 0; i < 8; ++i)
            {
                if (valsGpu[i] < iso[0])
                    cubeindex |= 1 << i;
            }

            if (EdgeTableStatic[cubeindex] == 0)
                return;

            PointGPU[] vertList = new PointGPU[12];

            // Find the vertices where the surface intersects the cube 
            for (int id = 1, count = 0; id < 2048; id *= 2, count++)
            {
                if ((edgeT[cubeindex] & id) > 0)
                    vertList[count] = VertexInterpolation(iso[0], p[count], p[count + 1], v[count], v[count + 1]);
            }

            // Create the triangle 
            for (int i = 0; triT[cubeindex, i] != -1; i += 3)
            {
                TriangleGPU triangle = new TriangleGPU(3);

                triangle.p[0] = vertList[triT[cubeindex, i]];
                triangle.p[1] = vertList[triT[cubeindex, i + 1]];
                triangle.p[2] = vertList[triT[cubeindex, i + 2]];

                tri[i] = triangle;
            }
        }
    }

    [Cudafy]
    public struct TriangleGPU
    {
        public PointGPU[] p;

        public TriangleGPU(int t)
        {
            p = new PointGPU[t];
        }
    }

    [Cudafy]
    public struct PointGPU
    {
        public double x;
        public double y;
        public double z;

        public PointGPU(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }   
}

ADDED : Execute is a static method as it should be but it could be called only from static. 添加 :Execute是应该使用的静态方法,但只能从静态调用。 In other case the line: 在其他情况下,该行:

CudafyModule km = CudafyTranslator.Cudafy();

doesn't work because it is not supported from non-static calles Execute . 不起作用,因为非静态调用Execute不支持它。

In other words, what fields or anything else should I create and fill in new static method to have an independent static entity to call execute? 换句话说,我应该创建哪些字段或其他任何内容并填写新的静态方法以具有独立的静态实体来调用execute?

Sorry for disinformation. 很抱歉提供虚假信息。 The problem was in just public void PolygoniseOnGpu(...) . 问题出在公共无效的PolygoniseOnGpu(...)中 It wasn't noted as static. 并没有指出它是静态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 静态方法会使类成为混合对象吗? - Does static methods make a class a hybrid? 我应该将我的私有类方法设为静态吗? - Should I make my private class methods static? 类与静态方法 - class vs static methods 加载 class 中的 static 字段 - load static fields in class 在类中使用静态字段 - using static fields in a class 没有字段的类中 static 方法和非 static 方法的差异 - Differents in static methods and non static methods in classes without fields 将几个字段作为参数传递给其他类中的方法,或传递`this`并使字段公开 - Passing several fields as parameters to methods in other class, or pass `this` and make the fields public 在类中有效使用静态字段和方法? 在构造函数或声明中初始化? 我需要指导 - Effective use of static fields and methods in a class? Initialised in the constructor or in declaration? I need guidance 如果字段和方法是静态的,为什么对一个类的实例创建新对象是没有意义的? [C#] - Why is it pointless/is it pointless to new an instance of a class if the fields and methods are static? [C#] 静态类构造函数和静态类字段的范围 - Scope of static class constructor and static class fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM