Bonjour.
je suis débutant dans le Java et je besoin d'une aide pour la multiplication parallèle des matrices.
Bonjour.
je suis débutant dans le Java et je besoin d'une aide pour la multiplication parallèle des matrices.
Bonjour.
j'arrive écrire des programmes parallèle , mais quand je veux le faire pour les matrices, je ne savais pas comment décomposer le traitement de calcule
Merci.
voici un code de multiplication de deux matrice :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 import java.util.*; import java.util.Random; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Entrez le nombre de rangées : "); int m = input.nextInt(); Random r = new Random(); System.out.print("Entrez le nombre de colonnes : "); int n = input.nextInt(); int[][] A = new int[m][n]; int[][] B = new int[m][n]; int[][] C = new int[m][n]; for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = r.nextInt(100) + 0; } for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = r.nextInt(100) + 0; } System.out.println("Matrix A : "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrice B : "); for(int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Le résultat est : "); System.out.println(); for(int i=0;i<A.length;i++){ for(int j=0;j<B[0].length;j++){ for(int k=0;k<A[0].length;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
mais j'arrive pas de l’écrire en parallèle " multi thread ", pour être exécuté par plusieurs thread
Par exemple, avant tu avais :
Et pour décomposer naïvement en 2 "tâches", tu pourrais avoir :
Code : Sélectionner tout - Visualiser dans une fenêtre à part for(int i=0;i<A.length;i++);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 for(int i=0;i<A.length/2;i++); for(int i=A.length/2;i<A.length;i++);
(Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)
N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
Merci pour ton aide.
mais j'arrive de le faire fonctionné
voici le mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package par; import java.util.Random; import java.util.Scanner; /** * * @author zerhem */ class MyThread extends Thread { private int debut, fin; private int[][] Aa = new int[100][100]; private int[][] Bb = new int[100][100]; MyThread(int d, int m, int[][] A, int[][] B){ this.debut = d; this.fin = m; this.Aa = A; this.Bb = B; this.start(); } public void run (){ for( int i = this.debut;i<this.fin;i++){ for(int j=0;j<Bb.length;j++){ for(int k=0;k<Aa.length;k++){ Par.C[i][j]+=Aa[i][k]*Bb[k][j]; } } } } } public class Par { public static int[][] C = new int[100][100]; /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); System.out.print("Entrez le nombre de rangées : "); int m = input.nextInt(); Random r = new Random(); System.out.print("Entrez le nombre de colonnes : "); int n = input.nextInt(); int[][] A = new int[m][n]; int[][] B = new int[m][n]; //int[][] C = new int[m][n]; for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = r.nextInt(10) + 1; } for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = r.nextInt(10) + 1; } System.out.println("Matrix A : "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrice B : "); for(int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println(); System.out.println(); System.out.println("Le résultat est : "); System.out.println(); int d = 0; MyThread myThread = new MyThread(d,A.length/2, A, B); Thread myThread1 = new MyThread(A.length/2,A.length, A, B); for(int i=0;i<A.length;i++){ for(int j=0;j<B.length;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
Ha bon, quelle erreur as-tu ? Moi j'ai lancé le code et il est passé, par contre tu n'as pas attendu la fin des Thread avant d'affiche le résultat![]()
(Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)
N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
il donne des mauvaise résultats , je pense que il manque la synchronisation .
pour que la matrice C affiche correctement.et je ne savais pas comment le faire
Oui voila je venais de le rajouter dans mon post, il manque un ou des join quelques part![]()
(Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)
N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
Salut
j'ai modifie le plupart de code, tu peux le vérifié es que est vraiment multi-thread
parce que je suis pas sure que il marche bien
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 /* /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package matr; import java.util.Random; import java.util.Scanner; /** * * @author zerhem */ public class moi extends Thread { private int[][] AA, BB; public int [][] CC; // private int debut, fin; // private Trieur parent; // private int nbNotify = 0; // public moi (int[][] A, int[][] B, int[][] C) { this(null, A, B, C, 0, A.length , B.length ); System.out.println("kjdqgvqkvq fin MOI(A,B) : " ); System.out.println("A.length = "+A.length ); System.out.println("AA.length = "+AA.length ); System.out.println("B.length = "+B.length ); System.out.println("BB.length = "+BB.length ); } public moi (Trieur parent, int[][] A, int[][] B, int[][] C, int debut, int fin, int BB) { System.out.println("Trieur parent, int[][] A, int[][] B, int debut, int fin, int BB : " ); this.parent = parent; this.AA = A; this.BB = B; this.CC = C; this.debut = debut; this.fin = fin; start(); } public synchronized void notifier() { // Notifie tous les thread en attente sur "ce" (this) moniteur // Attention, quand le message sera envoyé au parent (dans run()), // on incrémentera la variable nbNotify du parent (qui sera le this // implicite) et on notifiera le parent. // On peut aussi ne pas utiliser de méthode mais dans ce cas, il faut // écrire parent.nbNotify++; parent.notifyAll(); dans un bloc // synchronisé sur parent placé dans la méthode run. this.nbNotify++; this.notifyAll(); } public void run() { System.out.println("run : " ); for( int i = this.debut;i<this.fin;i++){ System.out.println("for 1 : " ); for(int j=0;j<BB.length;j++){ System.out.println("for 2 : " ); for(int k=0;k<AA.length;k++){ CC[i][j]+=AA[i][k]*BB[k][j]; System.out.println("for 3 : " ); } } } if (parent != null) { parent.notifier(); // indique qu'il a fini au parent qui l'attend } } public static void main(String[] args) { int m, n; Scanner input = new Scanner(System.in); System.out.print("Entrez le nombre de rangées : "); m = input.nextInt(); Random r = new Random(); System.out.print("Entrez le nombre de colonnes : "); n = input.nextInt(); int[][] A = new int[m][n]; int[][] B = new int[m][n]; int[][] C = new int[m][n]; for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = r.nextInt(10) + 1; } for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = r.nextInt(10) + 1; } System.out.println("Matrix A : "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrice B : "); for(int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); moi mo = new moi(A, B, C); try { mo.join(); } catch(InterruptedException e) {} for(int i=0;i<A.length;i++){ for(int j=0;j<B.length;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } System.out.println(); } }
Combien d'instance de la classe "moi" (donc de threads potentiel) crées tu ?
A priori 1, donc non ce n'est pas multithread.
La solution du dessus était, pour moi, la bonne si ce n'est qu'il manquait 2 join avant l'affichage des résultats afin d'attendre la fin du calcul de chacun des bouts de la matrice. (pas besoin d'autres synchronisation dans ce cas ci en plus).
Edit : Faut pas jouer avec des parent et de méthode notifier, je n'avais pas fait gaffe a cela au début, mais je pense pas que cela soit la bonne solution car quoiqu'il arrive même si ton architecture serait potentiellement bonne, chaque thread enfant attendrait la fin de son père si j'ai bien compris et donc ca ne serait pas exécuté en //
(Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)
N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
Merci pour ton aide.
j'ai corrigée le code par l’enlèvement l’implémentation des parent
c'est un code fonctionnel , je met ici pour les débutants comme moi
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package matr; import java.util.Random; import java.util.Scanner; /** * * @author zerhem */ public class moi extends Thread { private int[][] AA, BB; public int[][] CC; // private int debut, fin; // private Trieur parent; // private int nbNotify = 0; // public moi(int[][] A, int[][] B, int[][] C, int d, int f) { this.AA = A; this.BB = B; this.CC = C; this.debut = d; this.fin = f; System.out.println("kjdqgvqkvq fin MOI(A,B) : "); System.out.println("A.length = " + A.length); System.out.println("AA.length = " + AA.length); System.out.println("B.length = " + B.length); System.out.println("BB.length = " + BB.length); start(); } public void run() { System.out.println("run : "); for (int i = this.debut; i < this.fin; i++) { System.out.println("for 1 : "); for (int j = 0; j < BB.length; j++) { System.out.println("for 2 : "); for (int k = 0; k < AA.length; k++) { CC[i][j] += AA[i][k] * BB[k][j]; System.out.println("for 3 : "); } } } } public static void main(String[] args) { int m, n, d, f; Scanner input = new Scanner(System.in); System.out.print("Entrez le nombre de rangées : "); m = input.nextInt(); Random r = new Random(); System.out.print("Entrez le nombre de colonnes : "); n = input.nextInt(); int[][] A = new int[m][n]; int[][] B = new int[m][n]; int[][] C = new int[m][n]; int[][] Cs = new int[m][n]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[i].length; j++) { A[i][j] = r.nextInt(10) + 1; } } for (int i = 0; i < B.length; i++) { for (int j = 0; j < B[i].length; j++) { B[i][j] = r.nextInt(10) + 1; } } System.out.println("Matrice A : "); for (int i = 0; i < A.length; i++) { System.out.println(); for (int j = 0; j < A[i].length; j++) { System.out.print(A[i][j] + " "); } } System.out.println(); System.out.println(); System.out.println("Matrice B : "); for (int i = 0; i < B.length; i++) { System.out.println(); for (int j = 0; j < B[i].length; j++) { System.out.print(B[i][j] + " "); } } System.out.println(); System.out.println(); d = 0; f = A.length / 2; moi mo = new moi(A, B, C, d, f); d = A.length / 2; f = A.length; moi mo1 = new moi(A, B, C, d, f); try { mo.join(); mo1.join(); } catch (InterruptedException e) { } System.out.println("Le résultat est: "); for (int i = 0; i < A.length; i++) { for (int j = 0; j < B.length; j++) { System.out.print(+C[i][j] + " "); } System.out.println(); } } }
Partager