Laden...

Profil von wollmich

myCSharp.de - Member Mitglied seit
wollmich
myCSharp.de - Member
49
Themen
178
Beiträge
Dabei seit
20.06.2008
Letzte Aktivität
vor 8 Jahren
Alter
47 Jahre
Beruf
Dipl. El. Ing. FH
Herkunft
Bern (CH)
Erstellt vor 8 Jahren

Guten Tag,

ja ich kenne das Spiel. Und ich habe es aufgegeben. Ich schreibe die Settings wieder in die Registry unter

HKEY_CURRENT_USER\SOFTWARE\<Company>\<Product>

Siehe Microsoft.Win32.Registry.GetValue und Microsoft.Win32.Registry.SetValue.

Gruss Wollmich

Erstellt vor 8 Jahren

Guten Tag,

Da ich leider bis jetzt noch keine brauchbare Antwort bekommen habe, habe ich mir erlaubt die Frage auch noch in einem englisch sprachigem Forum zu stellen, siehe C# COM DLL with gcServer = true and gcAllowVeryLargeObjects = true.
Ich hoffe ich werde dafür nicht gehasst.

Ich weiss inzwischen, dass die Win32 C++ EXE eine app.config haben kann und diese auch benutzt wird.
<gcAllowVeryLargeObjects enabled="true" /> funktioniert.
<gcServer enabled="true"/> funktioniert nicht

Gruss Wollmich

Erstellt vor 8 Jahren

Guten Tag,

Ich habe folgendes Szenario:

  1. C# DLL (welche COM Visible ist)

  2. C# EXE welche die C# DLL braucht. Und in app.config <gcServer enabled="true"/>
    Bis hier funktioniert alles so wie ich es will.

  3. Win32 C++ EXE welche die die C# DLL über COM braucht. Geht auch.
    Aber wie kann ich <gcServer enabled="true"/> setzen?
    Damit in der C# DLL GCSettings.IsServerGC true ist?

Gruss Wollmich

Erstellt vor 11 Jahren

Der Ablauf vereinfacht dargestellt:

  1. dtrnlsp_init(ref handle, ref n, ref m, x, eps, ref iter1, ref iter2, ref rs)
  2. dtrnlsp_check(ref handle, ref n, ref m, fjac, fvec, eps, info)
  3. LOOP dtrnlsp_solve(ref handle, fvec, fjac, ref RCI_Request)
  4. dtrnlsp_get(ref handle, ref iter, ref st_cr, ref r1, ref r2)
  5. dtrnlsp_delete(ref handle)

In dtrnlsp_solve wird auch das Array x verändert. Kann es sein, dass dies die Memory Exception gibt? Wenn ja was kann ich tun?

Gruss Wollmich

Erstellt vor 11 Jahren

Der Beispiel Code in C#:

            /* nonlinear least square problem without boundary constraints */

            /* user's objective function */
            ObjectiveFunctionDelegate objective_function = extendet_powell;
            /* n - number of function variables
               m - dimension of function value */
            int n = 4, m = 4;
            /* precisions for stop-criteria (see manual for more detailes) */
            double[] eps = new double[6];
            /* solution vector. contains values x for f(x) */
            IntPtr x = new IntPtr(0);
            /* iter1 - maximum number of iterations
               iter2 - maximum number of iterations of calculation of trial-step */
            int iter1 = 1000, iter2 = 100;
            /* initial step bound */
            double rs = 0.0;
            /* reverse communication interface parameter */
            int RCI_Request;      // reverse communication interface variable
            /* controls of rci cycle */
            int successful;
            /* function (f(x)) value vector */
            IntPtr fvec = new IntPtr(0);
            /* jacobi matrix */
            IntPtr fjac = new IntPtr(0);
            /* number of iterations */
            int iter = 0;
            /* number of stop-criterion */
            int st_cr = 0;
            /* initial and final residuals */
            double r1 = 0, r2 = 0;
            /* TR solver handle */
            IntPtr handle = new IntPtr(0);   // TR solver handle
            /* cycle's counter */
            int i;
            /* results of input parameter checking */
            int[] info = new int[6];
            /* memory allocation flag */
            int mem_error, error;

            error = 0;
            /* memory allocation */
            mem_error = 1;
            x = _mkl.mkl_malloc(sizeof(double) * n, 64);
            if (x == IntPtr.Zero) goto end;
            fvec = _mkl.mkl_malloc(sizeof(double) * m, 64);
            if (fvec == IntPtr.Zero) goto end;
            fjac = _mkl.mkl_malloc(sizeof(double) * m * n, 64);
            if (fjac == IntPtr.Zero) goto end; 
            mem_error = 0;
            /* set precisions for stop-criteria */
            for (i = 0; i < 6; i++)
            {
                eps[i] = 0.00001;
            }
            /* set the initial guess */
            double[] x_init = new double[n];
            for (i = 0; i < n / 4; i++)
            {
                x_init[4 * i] = 3.0;
                x_init[4 * i + 1] = -1.0;
                x_init[4 * i + 2] = 0.0;
                x_init[4 * i + 3] = 1.0;
            }
            Marshal.Copy(x_init, 0, x, n);
            /* set initial values */
            double[] fvec_init = new double[m];
            Marshal.Copy(fvec_init, 0, fvec, m);
            double[] fjac_init = new double[m * n];
            Marshal.Copy(fjac_init, 0, fjac, m * n);
            /* initialize solver (allocate mamory, set initial values)
               handle       in/out: TR solver handle
               n       in:     number of function variables
               m       in:     dimension of function value
               x       in:     solution vector. contains values x for f(x)
               eps     in:     precisions for stop-criteria
               iter1   in:     maximum number of iterations
               iter2   in:     maximum number of iterations of calculation of trial-step
               rs      in:     initial step bound */
            if (_mkl.dtrnlsp_init(ref handle, ref n, ref m, x, eps, ref iter1, ref iter2, ref rs) != TR_SUCCESS)
            {
                /* if function does not complete successfully then print error message */
                System.Console.WriteLine("| error in dtrnlsp_init");
                /* Release internal MKL memory that might be used for computations.         */
                /* NOTE: It is important to call the routine below to avoid memory leaks   */
                /* unless you disable MKL Memory Manager                                   */
                _mkl.mkl_free_buffers();
                /* and exit */
                error = 1;
                goto end;
            }
            /* Checks the correctness of handle and arrays containing Jacobian matrix, 
               objective function, lower and upper bounds, and stopping criteria. */
            if (_mkl.dtrnlsp_check(ref handle, ref n, ref m, fjac, fvec, eps, info) != TR_SUCCESS)
            {
                /* if function does not complete successfully then print error message */
                System.Console.WriteLine("| error in dtrnlspbc_init");
                /* Release internal MKL memory that might be used for computations.         */
                /* NOTE: It is important to call the routine below to avoid memory leaks   */
                /* unless you disable MKL Memory Manager                                   */
                _mkl.mkl_free_buffers();
                /* and exit */
                error = 1;
                goto end;
            }
            else
            {
                if (info[0] != 0 || // The handle is not valid.
                    info[1] != 0 || // The fjac array is not valid.
                    info[2] != 0 || // The fvec array is not valid.
                    info[3] != 0    // The eps array is not valid.
                   )
                {
                    System.Console.WriteLine("| input parameters for dtrnlsp_solve are not valid");
                    /* Release internal MKL memory that might be used for computations.         */
                    /* NOTE: It is important to call the routine below to avoid memory leaks   */
                    /* unless you disable MKL Memory Manager                                   */
                    _mkl.mkl_free_buffers();
                    /* and exit */
                    error = 1;
                    goto end;
                }
            }
            /* set initial rci cycle variables */
            RCI_Request = 0;
            successful = 0;
            /* rci cycle */
            while (successful == 0)
            {
                /* call tr solver
                   handle               in/out: tr solver handle
                   fvec         in:     vector
                   fjac         in:     jacobi matrix
                   RCI_request in/out:  return number which denote next step for performing */
                if (_mkl.dtrnlsp_solve(ref handle, fvec, fjac, ref RCI_Request) != TR_SUCCESS)
                {
                    /* if function does not complete successfully then print error message */
                    System.Console.WriteLine("| error in dtrnlsp_solve");
                    /* Release internal MKL memory that might be used for computations.         */
                    /* NOTE: It is important to call the routine below to avoid memory leaks   */
                    /* unless you disable MKL Memory Manager                                   */
                    _mkl.mkl_free_buffers();
                    /* and exit */
                    error = 1;
                    goto end;
                }

                System.Console.WriteLine("RCI_Request: " + RCI_Request);

                /* according with rci_request value we do next step */
                if (RCI_Request == -1 ||
                    RCI_Request == -2 ||
                    RCI_Request == -3 ||
                    RCI_Request == -4 || RCI_Request == -5 || RCI_Request == -6)
                    /* exit rci cycle */
                    successful = 1;
                if (RCI_Request == 1)
                {
                    /* recalculate function value
                       m            in:     dimension of function value
                       n            in:     number of function variables
                       x            in:     solution vector
                       fvec    out:    function value f(x) */
                    objective_function(ref m, ref n, x, fvec);
                }
                if (RCI_Request == 2)
                {
                    /* compute jacobi matrix
                       extendet_powell      in:     external objective function
                       n               in:     number of function variables
                       m               in:     dimension of function value
                       fjac            out:    jacobi matrix
                       x               in:     solution vector
                       jac_eps         in:     jacobi calculation precision */
                    if (_mkl.djacobi(objective_function, ref n, ref m, fjac, x, eps) != TR_SUCCESS)
                    {
                        /* if function does not complete successfully then print error message */
                        System.Console.WriteLine("| error in djacobi");
                        /* Release internal MKL memory that might be used for computations.         */
                        /* NOTE: It is important to call the routine below to avoid memory leaks   */
                        /* unless you disable MKL Memory Manager                                   */
                        _mkl.mkl_free_buffers();
                        /* and exit */
                        error = 1;
                        goto end;
                    }
                }
            }
            /* get solution statuses
               handle            in:        TR solver handle
               iter              out:       number of iterations
               st_cr             out:       number of stop criterion
               r1                out:       initial residuals
               r2                out:       final residuals */
            if (_mkl.dtrnlsp_get(ref handle, ref iter, ref st_cr, ref r1, ref r2) != TR_SUCCESS)
            {
                /* if function does not complete successfully then print error message */
                System.Console.WriteLine("| error in dtrnlsp_get");
                /* Release internal MKL memory that might be used for computations.         */
                /* NOTE: It is important to call the routine below to avoid memory leaks   */
                /* unless you disable MKL Memory Manager                                   */
                _mkl.mkl_free_buffers();
                /* and exit */
                error = 1;
                goto end;
            }
            /* free handle memory */
            if (_mkl.dtrnlsp_delete(ref handle) != TR_SUCCESS)
            {
                /* if function does not complete successfully then print error message */
                System.Console.WriteLine("| error in dtrnlsp_delete");
                /* Release internal MKL memory that might be used for computations.         */
                /* NOTE: It is important to call the routine below to avoid memory leaks   */
                /* unless you disable MKL Memory Manager                                   */
                _mkl.mkl_free_buffers();
                /* and exit */
                error = 1;
                goto end;
            }
        /* free allocated memory */
        end:
            _mkl.mkl_free(ref fjac);
            _mkl.mkl_free(ref fvec);
            _mkl.mkl_free(ref x);
            if (error != 0)
            {
                return 1;
            }
            if (mem_error == 1)
            {
                System.Console.WriteLine("| insufficient memory");
                return 1;
            }
            /* Release internal MKL memory that might be used for computations.         */
            /* NOTE: It is important to call the routine below to avoid memory leaks   */
            /* unless you disable MKL Memory Manager                                   */
            _mkl.mkl_free_buffers();
            /* if final residual less then required precision then print pass */
            if (r2 < 0.00001)
            {
                System.Console.WriteLine("|         dtrnlsp powell............PASS");
                return 0;
            }
            /* else print failed */
            else
            {
                System.Console.WriteLine("|         dtrnlsp powell............FAILED");
                return 1;
            }
        }

        /* nonlinear system equations without constraints */
        /* routine for extendet powell function calculation
           m     in:     dimension of function value
           n     in:     number of function variables
           x     in:     vector for function calculating
           f     out:    function value f(x) */
        internal static void extendet_powell(ref int m, ref int n, IntPtr xp, IntPtr fp)
        {
            double[] x = new double[n];
            Marshal.Copy(xp, x, 0, n);
            double[] f = new double[m];
            Marshal.Copy(fp, f, 0, m);
            extendet_powell(ref m, ref n, x, f);
            Marshal.Copy(f, 0, fp, m);
        }

        internal static void extendet_powell(ref int m, ref int n, double[] x, double[] f)
        {
            for (int i = 0; i < n / 4; i++)
            {
                f[4 * i] = x[4 * i] + 10.0 * x[4 * i + 1];
                f[4 * i + 1] = 2.2360679774998 * (x[4 * i + 2] - x[4 * i + 3]);
                f[4 * i + 2] = (x[4 * i + 1] - 2.0 * x[4 * i + 2]) * (x[4 * i + 1] - 2.0 * x[4 * i + 2]);
                f[4 * i + 3] = 3.1622776601684 * (x[4 * i] - x[4 * i + 3]) * (x[4 * i] - x[4 * i + 3]);
            }
        }

und noch:


        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int djacobi(
            ObjectiveFunctionDelegate fcn, ref int n, ref int m, IntPtr fjac, IntPtr x, [In] double[] eps);

        internal static void mkl_free_buffers() { }

        //[DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        //internal static extern void mkl_free_buffers();

        internal static IntPtr mkl_malloc(int alloc_size, int alignment)
        {
            IntPtr p = new IntPtr(alloc_size);
            return mkl_malloc(ref p, ref alignment);
        }

        internal static IntPtr mkl_malloc(long alloc_size, int alignment)
        {
            IntPtr p = new IntPtr(alloc_size);
            return mkl_malloc(ref p, ref alignment);
        }

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern IntPtr mkl_malloc(ref IntPtr alloc_size, ref int alignment);

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern void mkl_free(ref IntPtr a_ptr);

und noch:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void ObjectiveFunctionDelegate(ref int m, ref int n, [In] IntPtr x, [In, Out] IntPtr f);

mkl_free_buffers wurde auskommentiert, da der Memory Manager von Intel MKL disabled wurde.
Der Fehler tritt bei (_mkl.dtrnlsp_solve(ref handle, fvec, fjac, ref RCI_Request auf. Der Fehler tritt nur machmal auf und nur bei grossen n und m (>1000).

Gruss Wollmich

Erstellt vor 11 Jahren

Guten Tag,

ich habe wieder mal ein Problem. Ich verwende Intel MKL aus C# und möchte ein Nonlinear Least Squares Problem without Constraints Problem lösen. Und habe folgendes Beispiel (ex_nlsqp_c.c) nach C# übersetzt.

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int dtrnlsp_init(
            ref IntPtr handle, ref int n, ref int m, IntPtr x, [In] double[] eps, ref int iter1, ref int iter2, ref double rs);

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int dtrnlsp_check(
            ref IntPtr handle, ref int n, ref int m, IntPtr fjac, IntPtr fvec, [In] double[] eps, [In, Out] int[] info);

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int dtrnlsp_solve(
            ref IntPtr handle, [In, Out] IntPtr fvec, [In] IntPtr fjac, ref int RCI_Request);

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int dtrnlsp_get(
            ref IntPtr handle, ref int iter, ref int st_cr, ref double r1, ref double r2);

        [DllImport("mkl", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
        internal static extern int dtrnlsp_delete(
            ref IntPtr handle);

dtrnlsp_solve gibt mir machmal die Fehlermeldung> Fehlermeldung:

Attempted to read or write protected memory Anmerkung dtrnlsp_solve verändert das Array x, welches bei dtrnlsp_init übergeben wird.

Irgendeine Idee?

Gruss Wollmich

Erstellt vor 11 Jahren

Vielen Dank für die Antwort 😃
Also so:

            int[] x = new int[] { 3, 5, 7, 5, 7, 9, 1, 3, 5 };
            HashSet<int> h = new HashSet<int>(x);
            int[] z = h.ToArray();
            Array.Sort(z);
            int[] y = new int[x.Length];
            for (int i = 0; i < x.Length; i++)
                y[i] = Array.FindIndex(z, delegate(int zz) { return zz == x[i]; }) + 1;

Wie kann ich jetzt noch das FindIndex umgehen?

EDIT: Mit einem Dictionary würde es gehen:

            int[] x = new int[] { 3, 5, 7, 5, 7, 9, 1, 3, 5 };
            HashSet<int> h = new HashSet<int>(x);
            int[] z = h.ToArray();
            Array.Sort(z);
            Dictionary<int, int> d = new Dictionary<int, int>(z.Length);
            for (int i = 0; i < z.Length; i++) d.Add(z[i], i + 1);
            int[] y = new int[x.Length];
            for (int i = 0; i < x.Length; i++)
                y[i] = d[x[i]];

Gibt es noch einen schnelleren Weg?

Erstellt vor 11 Jahren

Guten Tag,

angenommen ich habe folgendes int Array x:
3, 5, 7, 5, 7, 9, 1, 3, 5

und möchte folgenden Output y (gleiche Länge wie x):
2, 3, 4, 3, 4, 5, 1, 2, 3

und noch folgenden Output z (Länge = Anzahl verschiedene Elemente in x):
1, 3, 5, 7, 9

Wie löse ich so ein Problem so schnell wie möglich bei sehr grösser Länge (> 1 Million) von x?

Ansatz:
z = sortiere_und_lösche_duplikate(x);
yi = finde_index(xi, z) + 1;

Wie würdet Ihr das lösen?

Gruss Wollmich

Erstellt vor 11 Jahren

Guten Morgen
habe eine kurze Frage. Angenommen ich habe eine native DLL a und drei Assemblies b, c und d.

b ruft a mit dllimport auf. a ist als Content und Copy if Newer im Projekt von b.
c hat b als Reference
d hat c als Reference aber nicht b

Nun wird beim Build die native dll a nur in den bin Ordner von b und c kopiert aber nicht von d.

Wie kann ich das ändern? Damit ich a auch im bin Ordner von d habe, ohne b als Reference anzugeben.

Gruss Wollmich

Erstellt vor 11 Jahren

Danke mit Marshal.Copy und IntPtr geht es 😃